程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Only three lines of Python code are used to realize the import and export between the database and excel

編輯:Python

database ->Excel

Let's first take a look at the database department This department table . There are six pieces of data in this table , Represent different departments .

Let's take a look at this Python Code , First, import the library you need SQLAlchemy, This is a Python The most famous ORM Tools .

Full name Object Relational Mapping( Object relation mapping ).

Why use SQLAlchemy?

It can take your code from the underlying database and its related SQL Abstracted from features .

It's characterized by manipulation Python Object instead of SQL Inquire about , That is, at the code level, objects are considered , instead of SQL, It embodies a kind of procedural thinking , This makes Python The program is more concise and easy to read .

The specific use method is as follows :

from sqlalchemy import create_engine
import pandas as pd

Create database connection

engine = create_engine(‘mysql+pymysql://root:[email protected]/hong’)

Read mysql data

db = pd.read_sql(sql=‘select * from hong.department’, con=engine)

Export data to excel

db.to_excel(‘ Department data .xlsx’)

The first line of code is to first create a connection to the database .

my mysql User name is root, The password is 211314,

Because here I start the local database service , So it is localhost.

The slash is followed by the name of the database hong

The second line of code is to use pandas Of read_sql() Inquire about mysql surface department Data in

The second line of code is to pass the queried data through pandas Of to_excel() Write locally

The execution result is successfully written to the local server excel file

Excel-> database

Next, let's look at how to integrate local xlsx Data written to mysql In file .

from sqlalchemy import create_engine
import pandas as pd

Create database connection

engine = create_engine(‘mysql+pymysql://root:[email protected]/hong’)

Read xlsx file

df = pd.read_excel(‘ Analog data .xlsx’)

Import to mysql database

df.to_sql(name=‘test_data’, con=engine, index=False, if_exists=‘replace’)

Similarly, the first line of code is to first create a connection to the database

The second line uses pandas Of read_excel() Read local file . as follows :

This is for me python Of faker A hundred simulated data

The third step is to use pandas Of to_sql() Method writes the read data to mysql in

After the code is executed, return to mysql In my hong The database found one more test_data Table of .

Open it and have a look . Then this data is the same as the local data .

therefore . Here we use three lines of code from the database to excel Import data , Another three lines of code from excel Import data into the database .

Click for relevant information [ Here is the picture 005]https://docs.qq.com/doc/DU2t3eVlTdE9TekRJ

To sum up :

Two way data import , All are 3 One line of code .

From database to excel Import data :

1、 use sqlalchemy Create database connection

2、 use pandas Of read_sql Read data from the database

3、 use pandas Of to_csv Store data in csv file

from excel Import data into the database :

1、 use sqlalchemy Create database connection

2、 use pandas Of read_csv Read csv The data of

3、 use pandas Of to_sql Put the data in the database a


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved