0
votes

Here is my code:

  import mysql.connector
  import datetime
  import dateutil.parser
  import soundfile as sf

  mydb = mysql.connector.connect(
  host="localhost",
  user="py",
  password="12345678",
  database="mydatabase"
   )
  mycursor = mydb.cursor()
  sql = "INSERT INTO customers (adress) VALUES (%s)"
  val = ("Highway 21") 
  mycursor.execute(sql, val)

  mydb.commit()

  print(mycursor.rowcount, "record inserted.")

ProgrammingError Traceback (most recent call last) in

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 '%s)' at line 1

4

4 Answers

0
votes

This is due to MySQL INSERT statement syntax error. You can rewrite the query as follows:

val = "Highway 21"
sql = f'INSERT INTO customers (adress) VALUES ("{val}");'
print(sql)
# INSERT INTO customers (adress) VALUES ("Highway 21");
0
votes

You have to add a , after the "Highway 21" value since val needs to be a tuple because mycursor.execute() expects a list of values:

val = ("Highway 21",)
0
votes

you can also go through this code

sql = "INSERT INTO customers (adress) VALUES ('%s')"

val = ("Highway 21")

mycursor.execute(sql%(val))

-1
votes
val = ("Highway 21")
sql = "INSERT INTO customers (adress) VALUES ('{}') ".format(val)
print(sql)

use this you will not get error