python3连接MySQL数据库实例详解
作者:润青 时间:2024-01-12 22:47:37
本文实例为大家分享了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()
来源:https://www.cnblogs.com/nyist0/p/9082405.html
标签:python3,mysql
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python try-except-else-finally的具体使用
2022-10-18 14:31:45
![](https://img.aspxhome.com/file/2023/3/115643_0s.png)
Python基础 while循环与break、continue关键字
2023-11-19 03:41:42
![](https://img.aspxhome.com/file/2023/3/64283_0s.jpg)
python工具之清理 Markdown 中没有引用的图片
2022-02-14 11:44:39
![](https://img.aspxhome.com/file/2023/0/67300_0s.png)
Python 图像处理之PIL库详解用法
2023-05-02 18:06:50
![](https://img.aspxhome.com/file/2023/1/90671_0s.jpg)
python实现在列表中查找某个元素的下标示例
2023-08-20 07:16:38
![](https://img.aspxhome.com/file/2023/5/131155_0s.png)
python Flask框架之HTTP请求详解
2023-10-02 01:52:55
![](https://img.aspxhome.com/file/2023/3/135093_0s.png)
mysql查询字段类型为json时的两种查询方式
2024-01-27 19:05:48
![](https://img.aspxhome.com/file/2023/8/105678_0s.jpg)
Python configparser模块应用过程解析
2022-08-03 19:56:11
基于 Python 实践感知器分类算法
2022-08-08 05:49:31
如何通过shell脚本自动生成vue文件详解
2024-05-22 10:28:35
![](https://img.aspxhome.com/file/2023/1/123701_0s.jpg)
Python+OpenCV解决彩色图亮度不均衡问题
2023-02-08 23:14:53
![](https://img.aspxhome.com/file/2023/5/135135_0s.jpg)
python最长回文串算法
2023-03-05 02:27:37
Python使用爬虫猜密码
2023-03-21 12:38:39
![](https://img.aspxhome.com/file/2023/6/67766_0s.png)
Python 转换时间戳为指定格式日期
2023-11-05 13:48:41
asp访google分页代码
2009-07-10 13:06:00
ASP中正则表达式的应用
2010-05-27 12:24:00
Python数据可视化实现正态分布(高斯分布)
2022-10-28 03:31:29
![](https://img.aspxhome.com/file/2023/0/87780_0s.jpg)
Python 异常处理的实例详解
2023-10-05 17:11:58
Oracle数据库完整卸载的完整步骤
2024-01-13 13:15:30
![](https://img.aspxhome.com/file/2023/9/67529_0s.jpg)
详解如何用Golang处理每分钟100万个请求
2023-10-12 20:30:30
![](https://img.aspxhome.com/file/2023/8/103038_0s.webp)