Pysql插入EXCEL格式的数据
from pymysql import *
import pandas as pd
from sqlalchemy import create_engine
def InsertSQL(excel_name):
df = pd.read_excel(excel_name)
print('Start insert!'+excel_name)
connect = create_engine('mysql+pymysql://root:[email protected]:3306/Semir?charset=utf8')
pd.io.sql.to_sql(df,'ReturnRate_SKU', connect, schema='Semir', if_exists='append')
print(excel_name+'Insert All Done!')
def main():
excel_name = ['2019年12.xlsx','2019年34.xlsx','2019年56.xlsx','2019年78.xlsx','2019年910.xlsx','2019年11月1日-11月17日.xlsx','2019年11月18日-11月24日.xlsx']
for i in excel_name:
InsertSQL(i)
if __name__ == '__main__':
main()
Pysql查询
from pymysql import *
conn = connect(host = '127.0.0.1',port = 3306,user = 'root',password = 'Password',database = 'Semir',charset = 'utf8')
cs1 = conn.cursor()
count = cs1.execute('select * from ReturnRate_SKU_TRUE limit 500;')
for i in range (count):
result = cs1.fetchone()
print(result)