Python uses Pandas to import database sql

Python uses Pandas to import database sql

I. Introduction

For relational database access, the Python community has developed a standard called the Python Database API Specification. Specific database modules such as Mysql and Oracal all comply with this specification, so even for different databases such as sqlite, mysql, oracle, etc., Python can access them through the corresponding database adapter module, and the interface that these database modules follow Standards are uniform.

For the specific content of this aspect, you can refer to another blog post of mine:
Relational Database API Specification for Python Database Programming

2. Preparation

The database used in this example is MySql. It is assumed that you have installed the MySql database and created the corresponding tables.

For information about installing and using MySql on Windows systems, you can refer to my other blog post:
How to install and start MySql on Windows

Regarding how Python interacts with the MySql database, you can refer to another blog post of mine:
MySql database interaction based on Python

3. Import data from database to Pandas

Python needs to go through two steps to import data from the database to Pandas:

  • Python connects to the database

  • python execute sql query

Old way of writing (pandas will report a warning log after execution)

import pymysql
import pandas as pd

host = 'localhost'
user = 'root'
passwd = '123456'
db = 'mytestdb'

conn = pymysql.connect(host=host,
                       user=user,
                       passwd=passwd,
                       db=db,
                       charset='utf8')

sql = 'SELECT * FROM students'

pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
df = pd.read_sql(sql, conn)
print(df)

New way of writing (need to install the sqlalchemy library):

from sqlalchemy import create_engine
import pandas as pd

MYSQL_HOST = 'localhost'
MYSQL_PORT = '3306'
MYSQL_USER = 'root'
MYSQL_PASSWORD = '123456'
MYSQL_DB = 'mytestdb'

engine = create_engine('mysql+pymysql://%s:%s@%s:%s/%s?charset=utf8'
                           % (MYSQL_USER, MYSQL_PASSWORD, MYSQL_HOST, MYSQL_PORT, MYSQL_DB))

sql = 'SELECT * FROM students'

df = pd.read_sql(sql, engine)

pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)
print(df)

20221223234701

The reason for adding these two lines of pd.set_option is:

pd.set_option('display.unicode.ambiguous_as_wide', True)
pd.set_option('display.unicode.east_asian_width', True)

Because there are Chinese in our table, the characters occupied by Chinese are different from those occupied by English and numbers, so we need to call pd.set_option() to make the table aligned and displayed. If you are using Jupyter to run the code, Jupyter will automatically render a table, you don't need this setting.

Guess you like

Origin blog.csdn.net/hubing_hust/article/details/128425478