MySQL工具类

main
pzyexn7bj 6 months ago
parent ccc02b3b44
commit 42c4d2dc4f

@ -0,0 +1,32 @@
import pymysql
def connect_to_mysql():
return pymysql.connect(
host='localhost',
port=3306,
user='root',
password='root',
database='pythontest',
autocommit=True, # 自动提交
charset='utf8mb4'
)
def query_sql(sql):
conn = connect_to_mysql()
try:
cursor = conn.cursor()
cursor.execute(sql)
return cursor.fetchall()
finally:
conn.close()
def not_query_sql(sql):
conn = connect_to_mysql()
try:
cursor = conn.cursor()
cursor.execute(sql)
finally:
conn.close()
Loading…
Cancel
Save