warrenfitzhenry
warrenfitzhenry

Reputation: 2299

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?

Upvotes: 8

Views: 10105

Answers (2)

ZaxR
ZaxR

Reputation: 5165

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.

Upvotes: 15

Kyle Stuart
Kyle Stuart

Reputation: 111

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.

Upvotes: 2

Related Questions