You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
4.5KB

  1. # coding=utf-8
  2. import pymysql
  3. from dbutils.pooled_db import PooledDB
  4. # from dbutils.persistent_db import PersistentDB
  5. mysqlInfo = {
  6. "host": '47.98.125.47',
  7. "user": 'root',
  8. "passwd": 'NingdaKeji123!',
  9. "db": 'idc',
  10. "port": 3306,
  11. "charset": "utf8"
  12. }
  13. class ConnMysql(object):
  14. __pool = None
  15. def __init__(self):
  16. # 构造函数,创建数据库连接、游标
  17. self.coon = ConnMysql._get_mysql_conn()
  18. self.cur = self.coon.cursor(cursor=pymysql.cursors.DictCursor)
  19. # 数据库连接池连接
  20. @staticmethod
  21. def _get_mysql_conn():
  22. global __pool
  23. if ConnMysql.__pool is None:
  24. __pool = PooledDB(
  25. creator=pymysql,
  26. mincached=1,
  27. maxcached=5,
  28. maxconnections=6,
  29. maxshared=3,
  30. blocking=True,
  31. maxusage=None,
  32. setsession=[],
  33. ping=2,
  34. host=mysqlInfo['host'],
  35. user=mysqlInfo['user'],
  36. passwd=mysqlInfo['passwd'],
  37. db=mysqlInfo['db'],
  38. port=mysqlInfo['port'],
  39. charset=mysqlInfo['charset'])
  40. return __pool.connection()
  41. # 插入、修改、删除一条
  42. def sql_change_msg(self, sql):
  43. change_sql = self.cur.execute(sql)
  44. self.coon.commit()
  45. return change_sql
  46. # 查询一条
  47. def sql_select_one(self, sql):
  48. self.cur.execute(sql)
  49. select_res = self.cur.fetchone()
  50. return select_res
  51. # 查询多条
  52. def sql_select_many(self, sql, count=None):
  53. self.cur.execute(sql)
  54. if count is None:
  55. select_res = self.cur.fetchall()
  56. else:
  57. select_res = self.cur.fetchmany(count)
  58. return select_res
  59. # 释放资源
  60. def release(self):
  61. self.coon.close()
  62. self.cur.close()
  63. if __name__ == '__main__':
  64. [{'Tables_in_idc': 'gjc'},
  65. {'Tables_in_idc': 'gjc2'},
  66. {'Tables_in_idc': 'idc_dept'},
  67. {'Tables_in_idc': 'idc_project'}, {'Tables_in_idc': 'idc_project_check'},
  68. {'Tables_in_idc': 'idc_project_check_detail'}, {'Tables_in_idc': 'idc_project_module'},
  69. {'Tables_in_idc': 'idc_project_module_check'}, {'Tables_in_idc': 'idc_project_module_check_detail'},
  70. {'Tables_in_idc': 'idc_user'}, {'Tables_in_idc': 'idc_user_dept'}, {'Tables_in_idc': 'mk2'}]
  71. # print(ConnMysql().sql_select_many("show tables;"))
  72. mysql = ConnMysql()
  73. # 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"))
  74. # print(mysql.sql_select_many("""select * from gjc"""))
  75. # print(mysql.sql_select_many("""select * from gjc2 where id=dup_file_test"""))
  76. # print(mysql.sql_select_many("""select * from xmnr"""))
  77. # print(mysql.sql_select_many("""select * from gjc_copy1"""))
  78. # print(mysql.sql_select_one("""select * from idc_project_check"""))
  79. # print(mysql.sql_select_one("""select * from idc_project_check_detail"""))
  80. # print(mysql.sql_select_many("""select * from idc_project_module"""))
  81. # print(mysql.sql_select_many("""select * from idc_project_module where project_id=%d""" % int(7)))
  82. # print( mysql.sql_select_one("""select dup_id from idc_project_check where project_id=%d"""% int(7)))
  83. # print(len(mysql.sql_select_many("""select * from xmnr_copy1""")))
  84. # print(len(mysql.sql_select_many("""select * from user_history_data""")))
  85. print(len(mysql.sql_select_many("""select * from user_history_data""")))
  86. """查重复select * from user_history_module_data where gnms in (select gnms from user_history_module_data group by gnms having count(gnms)>1);
  87. """
  88. # print()
  89. # str_dict={}
  90. # cmnr_count=551
  91. # gnmkcount=1192
  92. #
  93. # print(mysql.sql_change_msg(
  94. # """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""" % (
  95. # str_dict.get('sbdw'), xmnr_count=551, gnmk_count=1192, 104)))
  96. # print(mysql.sql_change_msg(
  97. # """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""" % (
  98. # )
  99. # for k, v in mysql.sql_select_one("""select * from idc_project_check_detail""").items():
  100. # print(k, v)