时间:2021-07-01 10:21:17 帮助过:10人阅读
使用的是PyMySql组件
pip3 install PyMySQL
代码
#!/usr/bin/python3
import pymysql
# 打开数据库连接
conn = pymysql.connect (
host=‘127.0.0.1‘,
port=3306,
user=‘root‘,
password=‘12345‘,
database=‘mysql‘,
charset=‘utf8‘
)
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 返回字典数据类型
# 定义将要执行的sql语句
# sql = ‘select user from user;‘
sql = "SELECT user FROM user"
# 拼接并执行sql语句
cursor.execute(sql)
try:
cursor.execute(sql)
# 获取所有记录列表
results = cursor.fetchall()
for x in results:
print(x)
except:
print ("Error: unable to fetch data")
cursor.close()
conn.close()
python3 链接Mysql数据库
标签:ase -bash 返回 ror error: hal 执行sql sql imp