python连接mysql的代码(最基本的代码)

import pymysql


# 连接MySQL
# localhost/127.0.0.1 : 代表本地ip

db = pymysql.connect('localhost', 'root', 'nide', 'mydb1', 3306, charset='utf8')
# pymysql.connect('localhost', 'root', 'nide', 'mydb1')

# 创建游标: 作用是执行SQL语句
cursor = db.cursor()

# 执行SQL语句
sql = 'select * from student'
cursor.execute(sql)   # 执行sql语句

# 获取查询数据
print(cursor.fetchall())

cursor.close()   # 关闭游标
db.close()   # 关闭数据库

你可能感兴趣的:(Python,数据分析,mysql,python,数据库)