Skip to content

Instantly share code, notes, and snippets.

@gary136
Last active June 22, 2019 09:00
Show Gist options
  • Save gary136/6261af5a4a778d0fe92e99b26426f041 to your computer and use it in GitHub Desktop.
Save gary136/6261af5a4a778d0fe92e99b26426f041 to your computer and use it in GitHub Desktop.
Insert Table
# Insert Table
from sqlalchemy.exc import IntegrityError
y_m = y_m_generator(2010, 2018)
for y,m in y_m:
if m<10:
p = str(y)+'0'+str(m)
else:
p = str(y)+str(m)
# 爬蟲並轉成 data frame
df = monthly_sales(y,m)
# 將 data frame批次寫進資料庫
try:
df.to_sql(name='SII_REV_{}'.format(p), con=engine, if_exists='append', index=False)
# 若已有資料則不寫入
except IntegrityError:
print('catch duplicate data')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment