dbOperation.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import pymysql
  2. import db_config
  3. import re
  4. class dbOperation:
  5. #建立数据库连接,新建一个查询页面
  6. def __init__(self, dbName):
  7. # port要转为int
  8. # print("连接数据库")
  9. self.mysql = pymysql.connect(host = db_config.host # 连接名称,默认127.0.0.1 
  10. ,port = db_config.port # 端口,默认为3306
  11. ,user = db_config.user # 用户名
  12. ,passwd = db_config.passwd # 密码
  13. ,db = dbName # 数据库名称
  14. ,charset = db_config.charset # 字符编码
  15. )
  16. self.cursor = self.mysql.cursor(pymysql.cursors.DictCursor) # fetchone 是字典 fetchall是list套dict
  17. def table_exists(self,table_name): #这个函数用来判断表是否存在
  18. sql = "show tables;"
  19. self.cursor.execute(sql)
  20. tables = [self.cursor.fetchall()]
  21. table_list = re.findall('(\'.*?\')',str(tables))
  22. table_list = [re.sub("'",'',each) for each in table_list]
  23. if table_name in table_list:
  24. return 1 #存在返回1
  25. else:
  26. return 0 #不存在返回0
  27. def created_table(self, sql, tableName):
  28. self.cursor.execute("show tables;")
  29. tables = [self.cursor.fetchall()]
  30. tableList = re.findall('(\'.*?\')',str(tables))
  31. tableList = [re.sub("'",'',each) for each in tableList]
  32. if tableName in tableList:
  33. print("该表已经存在,无需创建")
  34. else:
  35. # self.cursor.execute("DROP " + tableName + " IF EXISTS EMPLOYEE") #创建表前 先删除表
  36. self.cursor.execute(sql)
  37. print("该表创建成功")
  38. #获取查询结果,结果是dict,上面建立游标指定了是dict
  39. def query_one(self, sql):
  40. self.cursor.execute(sql)
  41. data = self.cursor.fetchone()
  42. return data
  43. #获取查询结果,结果是dict,上面建立游标指定了是dict
  44. def query_list(self, sql):
  45. self.cursor.execute(sql)
  46. data = self.cursor.fetchall()
  47. return data
  48. def insert(self, sql):
  49. try:
  50. # 执行sql语句
  51. self.cursor.execute(sql)
  52. # 提交到数据库执行
  53. self.mysql.commit()
  54. except Exception as e:
  55. # Rollback in case there is any error
  56. # print("失败" + str(Exception))
  57. print ('str(e):\t\t', str(e)) #输出 str(e): integer division or modulo by zero
  58. self.mysql.rollback()
  59. def batchInsert(self, tableName, key, vlues):
  60. try:
  61. sql = "insert into `" + tableName + "`(" + key + ") values " + vlues +";"
  62. print(sql)
  63. # text = input("press any key to cancel csq \r\n")
  64. # 执行sql语句
  65. self.cursor.execute(sql)
  66. # 提交到数据库执行
  67. self.mysql.commit()
  68. except Exception as e:
  69. # Rollback in case there is any error
  70. # print("失败" + str(Exception))
  71. print ('str(e):\t\t', str(e)) #输出 str(e): integer division or modulo by zero
  72. self.mysql.rollback()
  73. def dele(self, sql):
  74. try:
  75. self.cursor.execute(sql) # 执行插入的sql语句
  76. self.mysql.commit() # 提交到数据库执行
  77. except:
  78. self.mysql.rollback()# 如果发生错误则回滚
  79. def update(self, sql):
  80. try:
  81. self.cursor.execute(sql) # 执行插入的sql语句
  82. self.mysql.commit() # 提交到数据库执行
  83. except:
  84. self.mysql.rollback()# 如果发生错误则回滚
  85. #关闭查询页面,关闭连接
  86. def close(self):
  87. self.cursor.close()
  88. self.mysql.close()