当前位置:网站首页>Pandas connection database read / write file
Pandas connection database read / write file
2022-06-10 02:49:00 【It's a fish, a small meatball, a duck】
install
Open the little skin , Create a database and table
sqlalchemy( To connect to the database , Use pip install sqlalchemy)
Use
import pandas as pd
# introduce pymysql
import pymysql
from sqlalchemy import create_engine
sql="select * from stu"
engine = create_engine('mysql+pymysql://root:[email protected]:3306/pandas')
res = pd.read_sql_query(sql, engine)
ressql="select * from stu"
stu Is the name of the table to be read from our database
create_engine('mysql+pymysql://root:[email protected]:3306/pandas')
root Is the user name
123456 It's the user password , Fill in your own database password
pandas Is the name of our database , You need to create it yourself
So we can read the data in our table
res = pd.read_sql_query(sql, engine)
read_sql_query Reading table ,sql It's the top sql sentence

If we want to write data to the database , Use the following command :
df.to_sql('user',engine)df Is the file to write ,user Is the table to write data to , The database cannot have duplicate tables
give an example :
Save the table data to the database , Then read out the data
import pandas as pd
# introduce pymysql
import pymysql
from sqlalchemy import create_engine
sql="select * from stu"
engine = create_engine('mysql+pymysql://root:[email protected]:3306/pandas')
res = pd.read_sql_query(sql, engine)
data=[[1,' Wang Yibo ','2103A',80],[2,' Xiao Zhan ','2103A',90],[3,' Delireba ','2103A',89]]
columns=[' Student number ',' full name ',' class ',' achievement ']
df=pd.DataFrame(data=data,columns=columns)
df.to_sql('user',engine)This writes the data to the database , Read out the data
# export
sqluser='select * from user'
use=pd.read_sql(sqluser,engine)
useBe careful : Write data and export data must be written separately , Otherwise, duplicate errors will be reported
边栏推荐
- Complex model machine experiment
- Taobao applet 2048 games
- Installation and use of numpy in pycharm
- Introduction to 51 single chip microcomputer stepping motor
- College entrance examination [activities]
- Technology dry goods | linkis practice: analysis of new engine implementation process
- yocto 构建image出现gstreamer源码编译错误,原因及解决方法
- 1. Getting started with pytorch
- Should a reliable and safe company open an account for US crude oil futures?
- 获取省市区的名称【项目 商城】
猜你喜欢
The process of manually installing redis extensions in yii2 framework

pandas连接数据库读写文件

双指针 | 283. 移动零
![College entrance examination [activities]](/img/da/b7e756a0a0298680142f62337f64b0.jpg)
College entrance examination [activities]

Introduction to 51 single chip microcomputer infrared communication

uni-app 移动端本地储存数据库sqlite,无存储限制

51 Introduction au micro - ordinateur à puce unique - - communication infrarouge

promise 介绍和实现
Basic principle and implementation of redis jump table

Double pointer | 27 Removing Elements
随机推荐
Double pointer | 27 Removing Elements
如何用Google Home音箱语音控制智汀家庭云设备?
pixi. JS tile background
51 Introduction au micro - ordinateur à puce unique - - communication infrarouge
How to use the salary management system?
大家用什么工具或平台来做接口性能测试
qiankun 如何提取出公共的依赖库
2022.05 ESP32 空中升级 OTA
Yocto实用技巧
51单片机入门——红外通信
2022.6.9 C asynchronous
Mysql database user permission management
3D reconstruction system | L2 camera model
Explain Seaborn in detail. Just read this one
Le premier élément de la Sous - collection est inséré par un autre tableau
Obtain the name of the province or city 【 project mall 】
How to hack user code gracefully
Timestamp transform to standard time format
openGauss“用户故事”正式上线!一键分享实践经验,限量版礼物等你拿
Double pointer | 283 Move zero