示例:注意['mysql+pymysql://root:123456@localhost:3306/python_db']書寫格式不要隨意加空格在中間
engine = create_engine('mysql+pymysql://root:123456@localhost:3306/python_db')
參數解釋:
dialect -- 數據庫類型
driver -- 數據庫驅動選擇
username -- 數據庫用戶名
password -- 用戶密碼
host 服務器地址
port 端口
database 數據庫
import pandas as pd
from sqlalchemy import create_engine
import pymysql
# 導入必要三模塊
# 查詢語句,選出customer2018表中的所有數據
sql = 'select * from customer2018;'
df = pd.read_sql_query(sql, engine)
# read_sql_query的兩個參數: sql語句, 數據庫連接
df = pd.read_sql_query(sql, engine)
# 輸出customer2018表的查詢結果
print(df)
runfile('D:/pyexcel/temppandas.py', wdir='D:/testl')
id creation_time customer ... quantity amout_of_money remarks
0 15 2021-04-14 17:46:25 aa ... NaN NaN None
1 16 2021-04-14 18:10:38 版本 ... 7.0 8.0 aaaa
2 17 NaT None ... NaN NaN None
[3 rows x 13 columns]