3

I have a table in pandas dataframe df.

   product_id_x    product_id_y    count       date
0   288472           288473          1     2016-11-08 04:02:07
1   288473           2933696         1     2016-11-08 04:02:07
2   288473           85694162        1     2016-11-08 04:02:07

i want to save this table in mysql database.

i am using MySQLdb package.

import MySQLdb
conn = MySQLdb.connect(host="xxx.xxx.xx.xx", user="name", passwd="pwd", db="dbname")

df.to_sql(con = conn, name = 'sample_insert', if_exists = 'append', flavor = 'mysql', index = False)

i used this query to put it in my db.

but i am getting error.

ValueError: database flavor mysql is not supported

my datatype is str for all the columns.

type(df['product_id_x'][0]) = str
type(df['product_id_y'][0]) = str
type(df['count'][0])        = str
type(df['date'][0])         = str

i don't want to use sqlalchemy or other packages, can anyone tell what's the error here. Thanks in advance

0

2 Answers 2

11

The flavor 'mysql' is deprecated in pandas version 0.19. You have to use the engine from sqlalchemy to create the connection with the database.

from sqlalchemy import create_engine
engine = create_engine("mysql+mysqldb://USER:"+'PASSWORD'+"@localhost/DATABASE")
df.to_sql(con=engine, if_exists='append', index=False)

When defining your engine, you need to specify your user, password, host and database. In your specific case, this should look like:

engine = create_engine("mysql+mysqldb://name:[email protected]/dbname")
Sign up to request clarification or add additional context in comments.

2 Comments

to_sql require a positional argument of name
pip install MySql-Python pip install SQLAlchemy
0

It appears the option you are using for flavor no longer exists. A brief look at the docs confirms this:

flavor : ‘sqlite’, default None

DEPRECATED: this parameter will be removed in a future version, as ‘sqlite’ is the only supported option if SQLAlchemy is not installed.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.