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.
21 lines
585 B
21 lines
585 B
import pymysql
|
|
|
|
def execute_query(query, data=None):
|
|
conn = pymysql.connect(host='localhost', port=3306, user='root', passwd='1234', db='project', charset='utf8')
|
|
cursor = conn.cursor()
|
|
try:
|
|
if data:
|
|
cursor.execute(query, data)
|
|
else:
|
|
cursor.execute(query)
|
|
if query.strip().lower().startswith("select"):
|
|
return cursor.fetchall()
|
|
else:
|
|
conn.commit()
|
|
except Exception as e:
|
|
print(e)
|
|
conn.rollback()
|
|
finally:
|
|
cursor.close()
|
|
conn.close()
|