Python3数据库操作包pymysql的操作方法
作者:Young_id 时间:2024-01-28 20:01:34
以下代码实现环境是mac系统,本地配置mysql服务端和navicat premium客户端,python环境是配置了pymysql的anaconda3。
首先,与数据库建立connection和进行操作的原理
(1)通过navicat premium创建testdataset数据库和库内数据表test:
CREATE TABLE `test` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(10) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
(2)在test数据表里添加数据项
(3)jupyter notebook里连接数据库,并对数据库进行操作
import pandas as pd
import datetime
import pymysql
#创建连接
conn = pymysql.connect(host='127.0.0.1', port=3306, user='root',
passwd='******', db='testdataset', charset='utf8')#passwd是本地mysql服务器密码
conn
#Output:<pymysql.connections.Connection at 0x11443e588>
#创建游标
cursor = conn.cursor()
cursor
#Output:<pymysql.cursors.Cursor at 0x11443e2e8>
#执行SQL,并返回受影响行数
effect_row = cursor.execute("select * from test")
effect_row
#Output:4
#获取剩余结果的第一行数据
r1=cursor.fetchone()
r1
#Output:(1, '李明', 18)
name='王天'
age=17
sql="select name,age from test where name='%s' and age='%s'" % (name,age)
row_count=cursor.execute(sql)
row_1 = cursor.fetchone()
print(row_count,row_1)
#Output:1 ('王天', 17)
conn.commit()
cursor.close()
conn.close()
总结
以上所述是小编给大家介绍的Python3数据库操作包pymysql的操作方法网站的支持!
来源:https://blog.csdn.net/cymy001/article/details/81049493
标签:python,pymysql,数据库,操作包
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Django权限控制的使用
2023-12-27 01:50:57
![](https://img.aspxhome.com/file/2023/9/85269_0s.png)
使用php shell命令合并图片的代码
2023-06-21 22:34:35
浏览器右下角弹出提示窗口
2008-10-30 12:37:00
Pycharm配置anaconda环境图文教程
2023-11-10 02:18:20
![](https://img.aspxhome.com/file/2023/1/125141_0s.png)
python字典key不能是可以是啥类型
2022-12-28 20:54:04
网站中美好的细节
2011-07-13 18:43:07
实战mysql导出中文乱码及phpmyadmin导入中文乱码的解决方法
2024-05-11 09:19:05
![](https://img.aspxhome.com/file/2023/0/126310_0s.jpg)
python2.7删除文件夹和删除文件代码实例
2023-06-02 14:41:17
发现一个不错的11px字体:PMingLiu
2008-09-06 12:49:00
五步让你成为GO 语言高手
2023-06-16 18:00:41
![](https://img.aspxhome.com/file/2023/3/86293_0s.jpg)
python嵌套try...except如何使用详解
2022-06-21 16:53:00
jupyter notebook保存文件默认路径更改方法汇总(亲测可以)
2023-01-12 09:05:07
![](https://img.aspxhome.com/file/2023/5/131635_0s.jpg)
python正则表达式之re.match()与re.search()的用法及区别
2022-05-14 13:21:46
详解Git 的 rebase 命令使用方法
2023-04-16 16:57:08
![](https://img.aspxhome.com/file/2023/0/123730_0s.jpg)
xorm根据数据库生成go model文件的操作
2024-01-16 00:29:22
使用WordPress发送电子邮件的相关PHP函数用法解析
2024-05-11 09:44:45
![](https://img.aspxhome.com/file/2023/5/125875_0s.png)
一个div层打开显示效果 js
2008-05-19 12:35:00
MySQL获取系统性能和状态代码
2024-01-13 10:56:18
opencv python 图像轮廓/检测轮廓/绘制轮廓的方法
2022-08-13 06:54:59
![](https://img.aspxhome.com/file/2023/4/77864_0s.png)
Python 操作Excel-openpyxl模块用法实例
2021-01-20 09:29:34