pymysql地表最强运行模板
import pymysql
MYSQL_CONFIG = {
'host': 'localhost',
'port': 3306,
'user': 'hg',
'passwd': '123456',
'db': 'cov',
'charset': 'utf8',
}
def get_conn(MYSQL_CONFIG):
conn = pymysql.connect(**MYSQL_CONFIG)
cursor = conn.cursor(pymysql.cursors.DictCursor)
return conn, cursor
def close_conn(conn, cursor):
if cursor:
cursor.close()
if conn:
conn.close()
def query(sql,*args):
conn,cursor = get_conn(MYSQL_CONFIG)
cursor.execute(sql,args)
res = cursor.fetchall()
close_conn(conn,cursor)
return res
sql = "SELECT * FROM history"
query(sql)