Python uses Pandas library to read and write MySQL database

Reprinted
by the author: But hope the wind and rain will come _jc

link: https://www.jianshu.com/p/238a13995b2b
Source:
The copyright of Jianshu belongs to the author. For commercial reprints, please contact the author for authorization, and for non-commercial reprints, please indicate the source.

This sharing will introduce how to use the Pandas library in Python to read and write MySQL databases. First of all, we need to know some knowledge about ORM

ORM technology

  Object-relational mapping technology, namely ORM (Object-Relational Mapping) technology, refers to the mapping of the table structure of the relational database to the object, and the object in the program is automatically persisted by using the metadata that describes the mapping between the object and the database. into a relational database.
  In Python, the best known ORM framework is SQLAlchemy. Typical ORM middleware in Java are: Hibernate, ibatis, speedframework.

SQLAlchemy

  SQLAlchemy is an open source software under the Python programming language. Provides SQL Toolkit and Object Relational Mapping (ORM) tools, released under the MIT License

      The SQLAlchemy module provides the create_engine() function to initialize the database connection. SQLAlchemy uses a string to represent the connection information:

' Database type + database driver name://username:password@machine address:port number/database name '

Pandas reads and writes MySQL databases

 We need the following three libraries to implement Pandas reading and writing MySQL database:

  • pandas
  • sqlalchemy
  • pymysql

  Among them, the pandas module provides the read_sql_query() function to query the database, and the to_sql() function to write to the database. There is no need to implement a new MySQL data table. s

       The qlalchemy module implements connections to different databases, while the pymysql module enables Python to manipulate MySQL databases.
  We will use the mydb database in the MySQL database and the employee table as follows:

      

 

  The following will introduce a simple example to show how to implement reading and writing to MySQL database in pandas:
1  import pandas as pd
 2  from sqlalchemy import create_engine
 3  #Initialize database connection , use pymysql module 4 # MySQL user: root, password: 147369, port: 3306, database: test 5 engine = create_engine( ' mysql+pymysql:// root:123456@localhost:3306/test ' )
 6 #Query statement, select all data in the employee table 7 sql = ''' select * from employee; ''' 8 # Two parameters of read_sql_query: sql statement, database Connection 9 df = pd.read_sql_query(sql, engine)
 10 #Output the query results of the employee table
 
 

 
 
11  print (df)
 12  
13  #Create a new DataFrame in pandas, with only id and num columns 
14 df = pd.DataFrame({ ' id ' : [1, 2, 3, 4], ' name ' : [ ' zhangsan ' , ' lisi ' , ' wangwu ' , ' zhuliu ' ]})
 15 #Store the newly created DataFrame as a data table in MySQL, without storing the index column 16 df.to_sql( ' mydf ' , engine, index= True)
 17 print ( ' 
 Read from and write to Mysql table successfully!')

operation result:

This shows that we have indeed written the newly created DataFrame in pandas to MySQL!

Write CSV file to MySQL

The above example implements reading and writing of MySQL database using Pandas library. We will introduce another example: writing CSV file to MySQL. The input.CSV file of the example is as follows

. . . something wrong with

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325344093&siteId=291194637