僅用Python三行代碼,實(shí)現(xiàn)數(shù)據(jù)庫(kù)和excel之間的導(dǎo)入導(dǎo)出!
↑?關(guān)注 + 星標(biāo)?,每天學(xué)Python新技能
后臺(tái)回復(fù)【大禮包】送你Python自學(xué)大禮包

數(shù)據(jù)庫(kù)->Excel

全稱Object Relational Mapping(對(duì)象關(guān)系映射)。
具體使用方法如下:
from?sqlalchemy?import?create_engine
import?pandas?as?pd
#?創(chuàng)建數(shù)據(jù)庫(kù)連接
engine?=?create_engine('mysql+pymysql://root:211314@localhost/hong')
#?讀取mysql數(shù)據(jù)
db?=?pd.read_sql(sql='select?*?from?hong.department',?con=engine)
#?導(dǎo)出數(shù)據(jù)到excel
db.to_excel('部門數(shù)據(jù).xlsx')


Excel->數(shù)據(jù)庫(kù)
from?sqlalchemy?import?create_engine
import?pandas?as?pd
#?創(chuàng)建數(shù)據(jù)庫(kù)連接
engine?=?create_engine('mysql+pymysql://root:211314@localhost/hong')
#?讀取xlsx文件
df?=?pd.read_excel('模擬數(shù)據(jù).xlsx')
#?導(dǎo)入到mysql數(shù)據(jù)庫(kù)
df.to_sql(name='test_data',?con=engine,?index=False,?if_exists='replace')


1、用sqlalchemy創(chuàng)建數(shù)據(jù)庫(kù)連接
2、用pandas的read_sql讀取數(shù)據(jù)庫(kù)的數(shù)據(jù)
3、用pandas的to_csv把數(shù)據(jù)存入csv文件
1、用sqlalchemy創(chuàng)建數(shù)據(jù)庫(kù)連接
2、用pandas的read_csv讀取csv的數(shù)據(jù)
3、用pandas的to_sql把數(shù)據(jù)存入數(shù)據(jù)庫(kù)
評(píng)論
圖片
表情


