2

So basically I have this list :

L1 = ['hel-lo', 'world123', 'bye', 'python']

I want to insert this list in my MySQL table, as follow :

+-------------------------+
|          words          |
+-------------------------+
| hel-lo                  |
| world123                |
| bye                     |
| python                  |
+-------------------------+

In reality, my list is composed of about 1000 elements. I tried many things which I found on StackOverflow, but nothing is working. (It seems that the elements are trying to be insert with this format "['string']").

Last solution I tried :

values = [list([item]) for item in L1]
cursor.executemany(u"INSERT INTO `table`(`data`) VALUES %s", values)

Which returns this error :

 mysql.connector.errors.ProgrammingError: 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''hel-lo')' at line 1

Any advices are welcomed !

6
  • 2
    dev.mysql.com/doc/connector-python/en/… Commented Aug 28, 2018 at 12:34
  • 1
    I already saw this link, what I am messing ? Commented Aug 28, 2018 at 12:38
  • 1
    Why you have in the table world123 and not world ? Commented Aug 28, 2018 at 12:38
  • @DavidWinder my mistake sorry Commented Aug 28, 2018 at 12:39
  • @DavidWinder Does it matter? Commented Aug 28, 2018 at 12:40

1 Answer 1

7

You're missing parentheses around the value list in the query (should be VALUES (%s)). Also, list([item]) can be simplified to just [item]:

L1 = ['hel-lo', 'world', 'bye', 'python']
values = [[item] for item in L1]
cursor.executemany(u"INSERT INTO `instability`(`ap_name`) VALUES (%s)", values)
Sign up to request clarification or add additional context in comments.

2 Comments

Shame on me, I just noticed that I wrote " % " instead of "(%)". I have no error now but nothing is inserted is the DB
You may need to commit the transaction.

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.