# coding=utf-8 import pymysql from dbutils.pooled_db import PooledDB # from dbutils.persistent_db import PersistentDB mysqlInfo = { "host": '47.98.125.47', "user": 'root', "passwd": 'NingdaKeji123!', "db": 'idc', "port": 3306, "charset": "utf8" } class ConnMysql(object): __pool = None def __init__(self): # 构造函数,创建数据库连接、游标 self.coon = ConnMysql._get_mysql_conn() self.cur = self.coon.cursor(cursor=pymysql.cursors.DictCursor) # 数据库连接池连接 @staticmethod def _get_mysql_conn(): global __pool if ConnMysql.__pool is None: __pool = PooledDB( creator=pymysql, mincached=1, maxcached=5, maxconnections=6, maxshared=3, blocking=True, maxusage=None, setsession=[], ping=2, host=mysqlInfo['host'], user=mysqlInfo['user'], passwd=mysqlInfo['passwd'], db=mysqlInfo['db'], port=mysqlInfo['port'], charset=mysqlInfo['charset']) return __pool.connection() # 插入、修改、删除一条 def sql_change_msg(self, sql): change_sql = self.cur.execute(sql) self.coon.commit() return change_sql # 查询一条 def sql_select_one(self, sql): self.cur.execute(sql) select_res = self.cur.fetchone() return select_res # 查询多条 def sql_select_many(self, sql, count=None): self.cur.execute(sql) if count is None: select_res = self.cur.fetchall() else: select_res = self.cur.fetchmany(count) return select_res # 释放资源 def release(self): self.coon.close() self.cur.close() if __name__ == '__main__': [{'Tables_in_idc': 'gjc'}, {'Tables_in_idc': 'gjc2'}, {'Tables_in_idc': 'idc_dept'}, {'Tables_in_idc': 'idc_project'}, {'Tables_in_idc': 'idc_project_check'}, {'Tables_in_idc': 'idc_project_check_detail'}, {'Tables_in_idc': 'idc_project_module'}, {'Tables_in_idc': 'idc_project_module_check'}, {'Tables_in_idc': 'idc_project_module_check_detail'}, {'Tables_in_idc': 'idc_user'}, {'Tables_in_idc': 'idc_user_dept'}, {'Tables_in_idc': 'mk2'}] # print(ConnMysql().sql_select_many("show tables;")) mysql = ConnMysql() # mysql.sql_change_msg("""insert into idc_project (project_name,file_path) value ('%s', '%s')""" % ("森林火险", "/opt/idc/file/20220924/79a53829-8965-4aof-a342-c532f6c9c2a3森林火险.xlsx")) # print(mysql.sql_select_many("""select * from gjc""")) # print(mysql.sql_select_many("""select * from gjc2 where id=dup_file_test""")) # print(mysql.sql_select_many("""select * from xmnr""")) # print(mysql.sql_select_many("""select * from gjc_copy1""")) # print(mysql.sql_select_one("""select * from idc_project_check""")) # print(mysql.sql_select_one("""select * from idc_project_check_detail""")) # print(mysql.sql_select_many("""select * from idc_project_module""")) # print(mysql.sql_select_many("""select * from idc_project_module where project_id=%d""" % int(7))) # print( mysql.sql_select_one("""select dup_id from idc_project_check where project_id=%d"""% int(7))) # print(len(mysql.sql_select_many("""select * from xmnr_copy1"""))) # print(len(mysql.sql_select_many("""select * from user_history_data"""))) print(len(mysql.sql_select_many("""select * from user_history_data"""))) """查重复select * from user_history_module_data where gnms in (select gnms from user_history_module_data group by gnms having count(gnms)>1); """ # print() # str_dict={} # cmnr_count=551 # gnmkcount=1192 # # print(mysql.sql_change_msg( # """update idc_project set company_name=%s, dup_status=3, one_vote_veto_status=dup_file_test, self_check_status=dup_file_test, history_project_count=%d ,module_count=%d where project_id=%d""" % ( # str_dict.get('sbdw'), xmnr_count=551, gnmk_count=1192, 104))) # print(mysql.sql_change_msg( # """update idc_project set dup_status=3, one_vote_veto_status=dup_file_test, self_check_status=dup_file_test, history_project_count=%d ,module_count=%d where project_id=%d""" % ( # ) # for k, v in mysql.sql_select_one("""select * from idc_project_check_detail""").items(): # print(k, v)