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

Shubham R
  • 7,382
  • 18
  • 53
  • 119

2 Answers2

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:pwd@xxx.xxx.xx.xx/dbname")
Marine
  • 408
  • 4
  • 10
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.

gold_cy
  • 13,648
  • 3
  • 23
  • 45