python基于Pandas读写MySQL数据库
作者:三只松鼠 时间:2024-01-16 18:34:19
目录
1、read_sql_query 读取 mysql
2、to_sql 写入数据库
要实现 pandas 对 mysql 的读写需要三个库
pandas
sqlalchemy
pymysql
可能有的同学会问,单独用 pymysql 或 sqlalchemy 来读写数据库不香么,为什么要同时用三个库?主要是使用场景不同,个人觉得就大数据处理而言,用 pandas 读写数据库更加便捷。
1、read_sql_query 读取 mysql
read_sql_query 或 read_sql 方法传入参数均为 sql 语句,读取数据库后,返回内容是 dateframe 对象。普及一下:dateframe 其实也是一种数据结构,类似 excel 表格一样。
import pandas
from sqlalchemy import create_engine
class mysqlconn:
def __init__(self):
mysql_username = 'root'
mysql_password = '123456'
# 填写真实数库ip
mysql_ip = 'x.x.x.x'
port = 3306
db = 'work'
# 初始化数据库连接,使用pymysql库
self.engine = create_engine('mysql+pymysql://{}:{}@{}:{}/{}'.format(mysql_username, mysql_password, mysql_ip, port,db))
# 查询mysql数据库
def query(self,sql):
df = pandas.read_sql_query(sql,self.engine)
# df = pandas.read_sql(sql,self.engine) 这种读取方式也可以
# 返回dateframe格式
return df
if __name__ =='__main__':
# 查询的 sql 语句
SQL = '''select * from working_time order by id desc '''
# 调用 mysqlconn 类的 query() 方法
df_data = mysqlconn().query(sql=SQL)
2、to_sql 写入数据库
使用 to_sql 方法写入数据库之前,先把数据转化成 dateframe 。
import pandas
from sqlalchemy import create_engine
class mysqlconn:
def __init__(self):
mysql_username = 'root'
mysql_password = '123456'
# 填写真实数库ip
mysql_ip = 'mysql.mall.svc.test.local'
port = 3306
db = 'work'
# 初始化数据库连接,使用pymysql库
self.engine = create_engine('mysql+pymysql://{}:{}@{}:{}/{}'.format(mysql_username, mysql_password, mysql_ip, port,db))
# 查询mysql数据库
def query(self,sql):
df = pandas.read_sql_query(sql,self.engine)
# df = pandas.read_sql(sql,self.engine)
# 返回dateframe格式
return df
# 写入mysql数据库
def to_sql(self,table,df):
# 第一个参数是表名
# if_exists:有三个值 fail、replace、append
# 1.fail:如果表存在,啥也不做
# 2.replace:如果表存在,删了表,再建立一个新表,把数据插入
# 3.append:如果表存在,把数据插入,如果表不存在创建一个表!!
# index 是否储存index列
df.to_sql(table, con=self.engine, if_exists='append', index=False)
if __name__ =='__main__':
# 创建 dateframe 对象
df = pandas.DataFrame([{'name':'小米','price':'3999','colour':'白色'},{'name':'华为','price':'4999','colour':'黑色'}])
# 调用 mysqlconn 类的 to_sql() 方法
mysqlconn().to_sql('phonetest',df)
插入数据库的数据:
来源:https://www.cnblogs.com/shenh/p/14542409.html
标签:Python,Pandas,MySQL
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
axios请求的一些常见操作实战指南
2023-07-02 16:33:25
基于Python代码实现Apriori 关联规则算法
2021-01-25 11:53:45
![](https://img.aspxhome.com/file/2023/1/87711_0s.jpg)
解决asp中ADODB.Stream 0x800A0C93 错误
2008-01-08 19:28:00
一篇文章快速了解Python的GIL
2021-04-23 00:25:23
![](https://img.aspxhome.com/file/2023/5/72245_0s.jpg)
python的三目运算符和not in运算符使用示例
2021-07-18 23:36:35
![](https://img.aspxhome.com/file/2023/0/77650_0s.png)
Python3.6+selenium2.53.6自动化测试_读取excel文件的方法
2023-04-03 18:41:41
![](https://img.aspxhome.com/file/2023/8/131388_0s.png)
python将视频转换为全字符视频
2023-08-08 21:01:02
![](https://img.aspxhome.com/file/2023/8/61748_0s.gif)
aspjpeg组件通用加水印函数代码
2011-04-04 10:32:00
python 多进程队列数据处理详解
2022-04-10 23:49:44
Python的@property的使用
2021-03-27 11:02:25
Python中的__init__作用是什么
2022-05-28 00:27:02
Python读取Pickle文件信息并计算与当前时间间隔的方法分析
2023-11-17 20:18:13
python 3.10上如何安装pyqt5
2022-04-09 23:59:53
![](https://img.aspxhome.com/file/2023/6/81866_0s.png)
MySQL 自定义函数CREATE FUNCTION示例
2024-01-26 00:48:07
JS统计Flash被网友点击过的代码
2024-05-03 15:06:18
深入分析Mysql中limit的用法
2024-01-20 12:56:08
matplotlib 纵坐标轴显示数据值的实例
2021-10-02 12:55:43
讲解SQL Server危险扩展存储删除和恢复
2008-12-09 14:30:00
在 CSS 中关于字体处理效果的思考
2008-04-25 22:57:00
![](https://img.aspxhome.com/file/UploadPic/20071/2007131103237363s.gif)
python实现简单爬虫功能的示例
2022-02-12 01:39:06
![](https://img.aspxhome.com/file/2023/7/133617_0s.png)