- 安裝mysqlconnector
pip install mysql-connector-2.1.6.tar.gz
- 安裝sqlalchemy
pip install sqlalchemy
- mysql的連線與操作
import mysql.connector
cnt = mysql.connector.connect(user='username', password='yourpassword', host='yourip', port=3306, database='dbname')
cursor_1 = cnt.cursor()
cursor_1.execute("selcect * from")
data = cursor_1.fetchall() #獲取結果
- dataframe直接寫入mysql
from sqlalchemy import create_engine
conn = create_engine('mysql+mysqlconnector://使用者名稱:密碼@ip:埠/資料庫?charset=utf8')
yourdf.to_sql(tablename, conn, schema='資料庫名', if_exists='append', index=False, chunksize=10000) # 如果表已經存在 df的列名應該與表裡的列名一樣