这是我的解决方案。这就是我使用的,它就像一个魅力!
import pandas as pd
import sqlalchemy as sql
import sqlalchemy.orm as orm
sh = pd.ExcelFile("ExcelFileName").parse("SheetName")
db=sql.create_engine('DBtype://username:password@localhost:postnumber/dbname')
metadata = sql.schema.MetaData(bind=db,reflect=True)
table = sql.Table('TableinDB', metadata, autoload=True)
class Row(object):
pass
rowmapper = orm.Mapper(Row,table)
print table.columns
Sess = orm.sessionmaker(bind = db)
session = Sess()
for x in range(len(sh.values)):
row1 = Row()
row1.field1 = sh.values[x][0]
row1.field2 = sh.values[x][1]
#Add as many fields as you want
session.add(row1)
#forloop ends
session.commit()