python3 链接Mysql数据库
使用的是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()
相关推荐
CloudXli 2020-06-04
JamesRayMurphy 2020-05-31
chuckchen 2020-10-31
Will0 2020-10-12
Dreamhome 2020-10-09
xirongxudlut 2020-09-28
星辰大海的路上 2020-09-13
chaochao 2020-08-31
猪猪侠喜欢躲猫猫 2020-08-17
快递小可 2020-08-16
shengge0 2020-07-26
巩庆奎 2020-07-21
张文倩数据库学生 2020-07-19
xirongxudlut 2020-07-18
Ericbig 2020-07-18
kyelu 2020-07-09
liangzhouqu 2020-07-07
GuoSir 2020-06-28