python讀寫mysql
阿新 • • 發佈:2018-11-10
- 讀取mysql資料
# -*- coding: utf-8 -*- # 匯入必要模組 import pandas as pd from sqlalchemy import create_engine # 初始化資料庫連線,使用pymysql模組 # MySQL的使用者:root, 密碼:147369, 埠:3306,資料庫:mydb engine = create_engine('mysql+pymysql://root:[email protected]:3306/student') # 查詢語句,選出employee表中的所有資料 sql = '''select * from data;''' # read_sql_query的兩個引數: sql語句, 資料庫連線 df = pd.read_sql_query(sql, engine,index_col='index') # 輸出employee表的查詢結果 print(df.shape)
- pandas寫入資料庫
# coding:utf-8 import pandas as pd from sqlalchemy import create_engine # 初始化資料庫連線,使用pymysql模組 # MySQL的使用者:root, 密碼:147369, 埠:3306,資料庫:mydb engine = create_engine('mysql+pymysql://root:[email protected]:3306/student') import datetime start = datetime.datetime.now().strftime('%Y-%m-%d') end = (datetime.datetime.now()+datetime.timedelta(days=100)).strftime('%Y-%m-%d') # 新建pandas中的DataFrame, 只有id,num兩列 # df = pd.DataFrame(data=np.random.randint(-100,100,(100,100)),index=pd.date_range('2018-1-1',periods=100,dtype='datetime64[ns]', freq='D'),columns=None,dtype=int)# print(df.shape) # 將新建的DataFrame儲存為MySQL中的資料表,不儲存index列 # df.to_sql('data', engine, if_exists='append',index= True) def write_sql(name,data=pd.DataFrame()): global engine data.to_sql(name,engine,if_exists='fail',index=True)
呼叫上面封裝方法:
# coding:utf-8 from mysql操作.write_SQL import write_sql import os import pandas as pd path = r'your data path' name_list = os.listdir(path)[0] for item in [name_list]: name = item.split('.')[0] path_ = os.path.join(path,item) print(path_) with open(path_, 'rb') as f: data = pd.read_csv(f, sep=',', header=None, index_col=None, na_values=['NULL'], na_filter=True, encoding='utf8') write_sql(name,data) print('打完收工!')