python3连接MySQL数据库实例详解
本文实例为大家分享了python3连接MySQL数据库的具体代码,供大家参考,具体内容如下
#python3连接MySQL实例
import pymysql
"""导入连接MySQL需要的包,没有安装pymysql需要先安装
使用命令行切换到python的安装路径下的scripts子目录下安装(pip install pymysql)
"""
#连接MySQL数据库
db = pymysql.connect("localhost","root","123456","python_conn",charset = 'utf8')
"""五个参数分别为 主机名,用户名,密码,所要连接的数据库名,为了解决中文乱码问题的字符集"""
#使用cursor()方法获取操作游标
cursor = db.cursor()
#使用execute()方法执行sql语句
sql = 'select * from test_conn'
cursor.execute(sql)
#使用fetchone()方法获取所有数据
results = cursor.fetchall()
#打印
for row in results:
sid = row[0]
name = row[1]
age = row[2]
sclass = row[3]
print(sid,name,age,sclass)
#关闭数据库
db.close() 相关推荐
JamesRayMurphy 2020-05-31
chuckchen 2020-10-31
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
chaigang 2020-06-27