1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
import pymysql from base_config import DB_HOST, DB_USER, DB_DATABASE, DB_PASSWORD, DB_PORT
class MysqlHelp(): """构造 def __init__(self, host=DB_HOST, user=DB_USER, port=DB_PORT, password=DB_PASSWORD, database=DB_DATABASE): self.host = host self.user = user self.port = port self.password = password self.db = database """ def open_coon(self,host=DB_HOST, user=DB_USER, port=DB_PORT, password=DB_PASSWORD, database=DB_DATABASE): self.coon = pymysql.connect(host=host, port=port, user=user, passwd=password, db=database) self.cursor = self.coon.cursor(cursor=pymysql.cursors.DictCursor)
def close(self): self.cursor.close() self.coon.cursor()
def insert_delete_update(self, sql, params=[]): try:
self.cursor.execute(sql, params) self.coon.commit()
return "OK" except Exception as erorr: return erorr
def select_fetchall(self, sql, params=[]): try:
self.cursor.execute(sql, params)
results = self.cursor.fetchall()
self.coon.commit()
return results
except Exception as erorr: return erorr
|