0

I have a data(results) that looks like this:

a column of lists

I would like to insert the data frame to a Mysql table (alarm):

What i have done so far:

session = Session()

for _, row in results_1.iterrows():
    
    insert_stmt = f"insert into alarm (service) values ({row['service']}) ;"

    session.execute(insert_stmt)

session.commit()
session.close

## insert to mysql 

DB_import_ref(db_table_name)

This keeps sending the error:

check the manual that corresponds to your MySQL server version for the right syntax to use near '['CS5', 'BE'])' at line 1")

Plz any help to insert a column of lists to Mysql ?

1 Answers1

0

You get a list, that has tio be converted to a string first before you enter it.

But you should take a god look at Is storing a delimited list in a database column really that bad? and reconsider your approach

for _, row in results_1.iterrows():

    str1 = ''.join(row['service'])
    
    insert_stmt = f"insert into alarm (service) values ({}) ;".format(str1)

    session.execute(insert_stmt)
nbk
  • 45,398
  • 8
  • 30
  • 47