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()will do the trick, although I find it to be very slow for large tables.