1

I have a pandas dataframe where one of the columns contains a list format tield (i.e. [2ndchance])

I want to insert it in a database as it is, but I am obtaining an error.

I ame executing the following sentence:

df_playbyplay.to_sql('playbyplay', con=engine, if_exists='replace', index=False)

and it fails wit the following error:

ProgrammingError: (mysql.connector.errors.ProgrammingError) Failed processing pyformat-parameters; Python 'list' cannot be converted to a MySQL type [SQL: 'INSERT INTO playbyplay

I would like to add it to the table as it is, if it is not possible just want to remove it from my dataframe, but whatever I execute it seems that always recognize it as a list while originally is just a string.

agm
  • 317
  • 3
  • 15

1 Answers1

1

List of variable each cell is not follow the safety of SQL rule, this type of insertion may cause SQL injection attacks.

What I recommend

df['list of columns']=df['list of columns'].str.join(', ')

Then we insert to database

BENY
  • 317,841
  • 20
  • 164
  • 234
  • Thanks for your answer, it is not exactly the case, I am scrapping a web and obtain a json, and one of the columns of the json contains in this format [2ndchance, pointsinthepaint], and as I want to insert it in a database when I use the command .to_sql I have problems with that specific field. So one of the solutions needed was just to remove the [] – agm Mar 30 '20 at 23:20