假设 ii 有一个数据框 df 并且它具有与 sql 表相同的列。将数据框插入表中的最简单(也是最有效的方法)是什么。
import pandas as pd
from sqlalchemy import create_engine
from sqlalchemy.ext.automap import automap_base
from sqlalchemy.orm import Session
df = pd.DataFrame({'name' : ['User 1', 'User 2', 'User 3']})
engine = create_engine('sqlite:///database.db')
conn = engine.connect()
Base = automap_base()
Base.prepare(engine, reflect=True)
session = Session(bind=engine)
# No assume that the database has a table Users with one column "name"
# is there a function, which adds all entries of the dataframe to the sql table?
# This should also works with dates/floats type of columns and multiple columns
最简单的方法?只需使用
df.to_sql('tableName', databaseConnectionObject, if_exists='append')
我认为它也是有效的,但不能说它是否最有效,你需要测试一下
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句