Store the data in DataFrame format into the mysql database

 

       Because the database is frequently operated recently, especially writing data is troublesome. After processing the data in DataFrame format or Series format, you will always be faced with writing data, and you can only perform format conversion. The to_sql() function was found during the search process. Baidu used it and realized it. The following record operation Process and problems encountered and solutions.

 First: Function introduction

 to_sql(frame, name, con, schema=None, if_exists='fail', index=True,
           index_label=None, chunksize=None, dtype=None)

frame: the name of the dataframe data

name: the table name of the database to be operated

con: statement to connect to the database, including (localhost, root, password, database)

if_exists:(fail,replace,append)

          fail: If the table exists, no operation is performed

          replace: If the table exists, delete the table, regenerate, and insert data

          append: insert data if the table exists, generate the table directly if it does not exist

Before using this function, we need to use create_engine

  •                                               
  • In the data, root: root is the user name and password, localhost is the address, and test is the database.
  • Because I have been using pymysql before, someone told me that I don’t need to install mysqldb, just mysql+pymysql. My experiment was successful, but maybe because it was successful before, you can try.

My environment is the Spyder tool of python3.6, because many people say that MySqlDb is no longer in the 3.6 environment, and they all let mysqlclient be installed. The installation here is version 1.3.13. The file at https://www.lfd.uci.edu/~gohlke/pythonlibs/ I downloaded directly during installation :

Because I found a lot of tutorials, I also installed mysql-python, which caused a version conflict. Later, I uninstalled and reinstalled mysqlclient to run successfully.

A useful function of Amway:

read_sql_query()

sql: The statement that needs to be operated,

con: database connection

Data in dataframe format can be read directly in the database, which is also convenient for reading operations and pandas analysis.

Second: Increase the Oracle version

Reference URL:

https://docs.sqlalchemy.org/en/13/dialects/oracle.html#module-sqlalchemy.dialects.oracle.cx_oracle

Code:

Guess you like

Origin blog.csdn.net/qq_28409193/article/details/81099724