from typing import Union, Any标签:__,self,mysql,list,host,sql,data From: https://www.cnblogs.com/littleyang/p/16769727.html
import pymysql
class Mysql:
def __init__(self, host):
self.host = host
self.port = 33066
self.user = ''
self.password = ''
self.charset = 'utf8'
def con_sql(self):
# 链接数据库并返回
return pymysql.connect(host=self.host,
port=self.port,
user=self.user,
password=self.password,
charset=self.charset)
def sql(self, sql_data):
# 创建游标
cs = self.con_sql().cursor()
# 执行sql语句
cs.execute(sql_data)
# 关闭sql连接
cs.close()
return cs.fetchall()
# 分表轮询默认100张表,返回存在数据的表id列表,根据id轮询查出对应数据
def range_sql(self, sql_data_first, sql_data_two, num=100):
table = []
list_str = []
for i in range(num):
context = self.sql(sql_data_first + str(i) + ' ' + sql_data_two)
if context:
table.append(i)
for d in table:
list_d = self.sql(sql_data_first + str(d) + ' ' + sql_data_two)
list_str.append(list_d[0][0])
return list_str
if __name__ == "__main__":
sql = Mysql('')