[Interaction between MySQL and python] 01 Connect to the database

import pymysql

'''
连接数据库
参数1:mysql服务所在主机IP
参数2:用户名
参数3:密码
参数4:要连接的数据库名称
'''
db = pymysql.connect('本机ip', 'root', 'root', 'aimei')

# 创建一个cursor对象
cursor = db.cursor()

sql = 'select version()'

# 执行sql语句
cursor.execute(sql)

# 获取返回的信息
data = cursor.fetchone()

print(data)

# 断开
cursor.close()
db.close()

 

The native ip can be obtained using ipconfig

 

If the local ip connection reports an error, find the user in the mysql database and change to%

 

Published 105 original articles · praised 104 · 10,000+ views

Guess you like

Origin blog.csdn.net/weixin_38114487/article/details/105446268