1 def get_conn():
2 """
3 :return: 连接,游标
4 """
5 # 创建连接
6 conn = pymysql.connect(host="127.0.0.1",
7 user="root",
8 password="你的数据库密码",
9 db="要使用的数据名称",
10 charset="utf8")
11 # 创建游标
12 cursor = conn.cursor() # 执行完毕返回的结果集默认以元组显示
13 return conn, cursor
14
15 def close_conn(conn, cursor):
16 if cursor:
17 cursor.close()
18 if conn:
19 conn.close()
20 """
21 -----------------------------------------------------------
22 """
23 """
24 ------------------------------------------------------------------------------------
25 """
26 def query(sql,*args):
27 """
28 通用封装查询
29 :param sql:
30 :param args:
31 :return:返回查询结果 ((),())
32 """
33 conn , cursor= get_conn()
34 print(sql)
35 cursor.execute(sql)
36 res = cursor.fetchall()
37 close_conn(conn , cursor)
38 return