简体   繁体   中英

Pandas df to database using flask-sqlalchemy

I'm trying to insert a pandas dataframe into a mysql database. I am using flask-sqlalchemy.

I have created this table:

class Client_Details(db.Model):
    __tablename__ = "client_history"

    client_id = db.Column(db.Integer, primary_key=True)
    client_name = db.Column(db.VARCHAR(50))
    shack= db.Column(db.VARCHAR(50))

and I would like to insert the data from this df into it:

index   name     shack
0        jay       H9
1        ray       I8
2        t-bop     I6
3        jay-k     F89
4        phil      D89

This doesn't seem to work:

for index, row in df.iterrows():
    client_add = client_history(client_name = row[1], shack =row[2])
    db.session.add(client_add)
    db.session.commit()

Is there a better way to do this, using to_sql , perhaps?

df.to_sql(engine, 'client_history') 

Is what you are looking for. There is no lighting fast way to do this (that I am aware of) but typically using builtin pandas functions is going to be faster than any solution that you can create on your own.

Kyle's answer was close - the flask-sqlalchemy engine IS created behind the scenes with some magic, but the correct way to access it is with db.engine. Here's what you're looking for:

df.to_sql(name='client_history', con=db.engine, index=False)

I also agree with VinceP's assessment that to_sql can be slow for larger tables, so keep that in mind.

For what it's worth, you can also access the session with Flask-SQLAlchemy as db.session.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM