0
import mysql.connector
conn = mysql.connector.connect(host="localhost",user="root",password="password", database="database_name")
cursor = conn.cursor()
a = "abcd"
cursor.execute("INSERT INTO table_jeux (lien_fiche) VALUES (?)", (a))

And this is the error I get when I execute the script :

ProgrammingError: 1064 (42000): Syntax error near to '?)' at line 1

Thanks for help, I really don't understand why.

2 Answers 2

1

What You probably want is to insert variable a into Your SQL code, but it doesn't work - and the parser gets a "?" where You want it to have "abcd"

You could try this:

cursor.execute("INSERT INTO table_jeux (lien_fiche) VALUES ({})".format(a))

or (in somewhat python2 manner):

cursor.execute("INSERT INTO table_jeux (lien_fiche) VALUES (%s)", (a,))

as described here: https://dev.mysql.com/doc/connector-python/en/connector-python-api-mysqlcursor-execute.html

Sign up to request clarification or add additional context in comments.

1 Comment

The second one is working perfectly, I try it but it was without the comma. Thanks.
0

Try using f strings,

It should look something like this:

cursor.execute(f'INSERT INTO student (student_name, student_email, courseid) VALUES ("{student_name}","{student_email}",{course_id})') 

where student_name, student_email and course_id are all variables.

In your case:

cursor.execute(f'INSERT INTO table_jeux (lien_fiche) VALUES ("{a}")')

3 Comments

Maybe you are using Python 2?
Yes I am using python 2
Ah, that's the problem. Should have warned you that f strings are only available in Python 3. My bad, I won't try to find another solution as Ergaro has already answered.

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.