学习python之编写简单简单连接数据库并执行查询操作
作者:mdxy-dxy 时间:2024-01-23 05:50:13
python 连接数据库操作, 方法如下:
在本机的mysql 数据库中有一个名为yao的库,其中有一个名为user的表,表中的内容如图
下面,则是python连接数据库的方法,及查找出表中的内容,代码如下:
#! /usr/bin/python
# filename conn.py
import MySQLdb # 载入连接数据库模块
try: # 尝试连接数据库
conn = MySQLdb.connect("localhost","root","www","yao",charset="utf8") # 定义连接数据库的信息
except MySQLdb.OperationalError, message: # 连接失败提示
print "link error"
cursor=conn.cursor() #定义连接对象
cursor.execute("select * from user") #使用cursor提供的方法来执行查询语句
data=cursor.fetchall() #使用fetchall方法返回所有查询结果
print data #打印查询结果
cursor.close() #关闭cursor对象
conn.close() #关闭数据库链接
程序执行结果为
标签:python,连接数据库,执行查询
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决Vue axios post请求,后台获取不到数据的问题方法
2024-05-09 09:38:38
Python中导入模块的几种方式总结
2023-08-04 17:03:54
![](https://img.aspxhome.com/file/2023/2/61432_0s.png)
Python 用三行代码提取PDF表格数据
2023-10-08 18:54:57
![](https://img.aspxhome.com/file/2023/9/99459_0s.jpg)
如何用python写一个简单的词法分析器
2022-08-02 00:11:11
![](https://img.aspxhome.com/file/2023/1/94121_0s.png)
Python进行区间取值案例讲解
2021-10-23 15:17:34
![](https://img.aspxhome.com/file/2023/5/70115_0s.png)
关于textarea的直观换行
2010-03-18 15:59:00
深入理解Java线程编程中的阻塞队列容器
2024-05-13 10:37:58
![](https://img.aspxhome.com/file/2023/3/125463_0s.png)
Python OpenCV实现图片上输出中文
2021-07-01 15:00:11
![](https://img.aspxhome.com/file/2023/3/115003_0s.jpg)
PyCharm中鼠标悬停在函数上时显示函数和帮助的解决方法
2023-08-10 19:20:02
![](https://img.aspxhome.com/file/2023/6/69606_0s.png)
MySQL中两种快速创建空表的方式的区别
2008-12-17 14:34:00
Windows server 2008 r2上安装MySQL5.7.10步骤
2024-01-15 06:47:11
![](https://img.aspxhome.com/file/2023/4/79014_0s.png)
pip安装Python库时遇到的问题及解决方法
2023-06-20 14:00:01
![](https://img.aspxhome.com/file/2023/1/100961_0s.png)
python处理二进制数据的方法
2022-09-08 06:20:09
VMWare linux mysql 5.7.13安装配置教程
2024-01-14 04:17:43
![](https://img.aspxhome.com/file/2023/9/93939_0s.png)
Ubuntu中MySQL的参数文件my.cnf示例详析
2024-01-13 02:26:28
git验证线上的版本是否符合预期
2023-03-04 16:24:09
![](https://img.aspxhome.com/file/2023/4/132414_0s.png)
Python爬取数据保存为Json格式的代码示例
2022-10-13 17:11:36
Jinja2过滤器的使用、控制语句示例详解
2023-01-30 19:02:00
![](https://img.aspxhome.com/file/2023/1/104911_0s.png)
python+pillow绘制矩阵盖尔圆简单实例
2022-07-04 02:44:39
![](https://img.aspxhome.com/file/2023/8/80428_0s.jpg)
得到自增列的下一个会插入的id
2024-01-20 17:32:48