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.

25 lines
643 B

11 months ago
11 months ago
11 months ago
  1. import pymysql
  2. DB_CONFIG = {
  3. "host": "111.229.38.129",
  4. "user": "root",
  5. "password": "zl981023",
  6. "database": "blogapi",
  7. "charset": "utf8mb4",
  8. "cursorclass": pymysql.cursors.DictCursor,
  9. }
  10. # 创建数据库连接
  11. def create_connection():
  12. return pymysql.connect(**DB_CONFIG)
  13. # 执行 SQL 查询
  14. def execute_query(query, params=None, fetchall=False):
  15. conn = create_connection()
  16. with conn.cursor() as cursor:
  17. cursor.execute(query, params)
  18. if fetchall:
  19. result = cursor.fetchall()
  20. else:
  21. result = cursor.fetchone()
  22. conn.commit()
  23. conn.close()
  24. return result