Python programmers must have skills: how to connect to the database in Python to obtain data

hello guys!

To become a data analyst, one of the most basic skills is: extracting data.

Data is the foundation of data analysis. Therefore, mastering various skills to obtain data must be an essential skill for a qualified data analyst. The data we need to extract is usually stored in the database (of course, there are also those that are not stored in the database, which is not the focus of discussion here).

Now, with the popularity of the python language, coupled with Python's powerful three-party library and ecosystem, almost allowing data analysts to complete the entire data analysis work, Python has almost become a programming language that every data analyst must know. Naturally, python has many third-party libraries that can connect to the database to facilitate data analysts to extract data from the database to complete the "one-stop service". So, how to use python to connect to the data database to successfully submit data?

We use the MySQL database for demonstration here, because in fact almost all the data extraction process is the following process, so you can use python to connect to the MySQL database to extract data, then, when you need to connect to other databases to extract data, you will always "change the soup Change the medicine".

​ Python connects to the database to get data

1. Establish a connection

To connect to the MySQL database in python, you need to import the pymysql module.

module installation

pip install pymysql      

Supplement: If it is in the jupyter environment, please use

!pip install pymysql

After installation, you can connect to the database.

#导入模块
import pymysql
#建立连接
conn = pymysql.connect(
    user='root',    # 用户名
    password='你的密码',   # 密码:这里一定要注意123456是字符串形式
    host='localhost',    # 指定访问的服务器,本地服务器指定“localhost”,远程服务器指定服务器的ip地址
    database='school',   # 数据库的名字
    port=3306,            # 指定端口号,范围在0-65535
    charset='utf8mb4',    # 数据库的编码方式
)

In this way, the bridge between python and MySQL is built. All that's left to do is extract the data.

2. Use the pandas library for data reading

Pandas is very powerful among the three artifacts of python data analysis. It can not only read excel spreadsheets, csv, text and other types of data files, but also directly provide the function of connecting to MySQl. Therefore, we can directly extract data by using the read_sql function in pandas (here I read the information of the student table in a local database school as an example)

the code

# 导入pandas
import pandas as pd      # 进行别名

# 一般只要需要指定两个主要的参数sql和con,sql是sql查询语句,con指定上面建立的连接
df = pd.read_sql('SELECT * FROM school.tb_student',con=conn)    
df

It's easy to read the data in the database here, isn't it super simple?

Finally, do things from the beginning to the end, and don't forget to close the connection after getting the data!

3. Close the connection

# 关闭连接
conn.close()

Well, today's sharing is over here

If it is helpful to you, you can also get more information, click the card below to scan the code

我都准备好了,包含数百本电子书,Python视频教程、源码等等

Guess you like

Origin blog.csdn.net/fei347795790/article/details/129299770