How to use Python to connect to Oracle, MySql and PostgreSQL databases?

Step 1: Install the necessary libraries

To connect to the database and query data, you need to install the following two libraries:

  1. pandas : A Python library for data analysis, including functions for reading and writing data.
  2. sqlalchemy : A Python library for manipulating relational databases, allowing you to use Python to interact with various databases.

You can install these libraries in Command Prompt or Terminal with the following commands:

pip install pandas
pip install sqlalchemy

Step 2: Connect to the database

Connecting to the database requires the following information:

  1. Database type : the type of database you want to connect to, such as MySQL, PostgreSQL, etc.
  2. Hostname : The hostname or IP address where the database is located.
  3. Port number : The port number of the database, usually the default port number.
  4. Username : The username required to connect to the database.
  5. Password : The password required to connect to the database.
  6. Database Name : The name of the database you want to connect to.

You can connect to the database with the following Python code:

from sqlalchemy import create_engine

# 连接到MySQL数据库
engine = create_engine('mysql://username:password@hostname:port/databasename')

# 连接到PostgreSQL数据库
engine = create_engine('postgresql://username:password@hostname:port/databasename')

# 连接到Oracle数据库,需要安装cx_Oracle库
engine = create_engine('oracle+cx_oracle://username:password@hostname:port/databasename')

Step 3: Use the read_sql function to query data

Use the read_sql function of pandas to query the data in the database. The read_sql function takes two parameters:

  1. SQL query : The SQL query you want to execute.
  2. Database Connection : The database connection you created earlier.

Here is an example query:

import pandas as pd

# 执行SQL查询并将结果存储在DataFrame中
df = pd.read_sql('SELECT * FROM mytable', engine)

# 打印DataFrame
print(df)

Guess you like

Origin blog.csdn.net/xili1342/article/details/130085430