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.
27 lines
644 B
27 lines
644 B
import pymysql
|
|
|
|
DB_CONFIG = {
|
|
"host": "111.229.38.129",
|
|
"user": "root",
|
|
"password": "zl981023",
|
|
"database": "blogapi",
|
|
"charset": "utf8mb4",
|
|
"cursorclass": pymysql.cursors.DictCursor,
|
|
}
|
|
|
|
# 创建数据库连接
|
|
def create_connection():
|
|
return pymysql.connect(**DB_CONFIG)
|
|
|
|
# 执行 SQL 查询
|
|
def execute_query(query, params=None, fetchall=False):
|
|
conn = create_connection()
|
|
with conn.cursor() as cursor:
|
|
cursor.execute(query, params)
|
|
if fetchall:
|
|
result = cursor.fetchall()
|
|
else:
|
|
result = cursor.fetchone()
|
|
conn.commit()
|
|
conn.close()
|
|
return result
|