python 连接数据库
时间:2021-07-01 10:21:17
帮助过:51人阅读
import pymysql
2
3
4 def mysql_db(sql):
5 # 连接数据库
6 db = pymysql.Connect(host=
‘47.**.***.245‘, port=3306, user=
‘***‘, password=
‘***‘, db=
‘***‘,
7 charset=
‘utf8‘)
8 # 创建游标
9 cursor =
db.cursor()
10 # 执行sql
11 cursor.execute(sql)
12 # 接收sql执行结果
13 data =
cursor.fetchall()
14 print(data)
15 for i
in data:
16 print(i)
17 db.close()
18
19
20 if __name__ ==
‘__main__‘:
21 sql =
‘select * from persion‘
22 mysql_db()
python 连接数据库
标签:word from 执行sql sele *** fetchall imp host def