Search code examples
pythonmysql-connector-python

PYTHON MYSQL INSERT ONE SINGLE VALUE


This is not working any id why? mysql.connector.errors.ProgrammingError: 1064 (42000): Synthax error near '%s)' at line 1

insert into a 2 fields table with a autoincrement key I just try to insert value


import mysql.connector

mydb = mysql.connector.connect(
  host="localhost",
  port=3306,
  user="**",
  passwd="******",
  database="db1"
)


mycursor = mydb.cursor()


sql = "INSERT INTO t_user (name) VALUES (%s)"
val = ('test')

mycursor.execute(sql, val)

mydb.commit()


Thank you


Solution

  • Statement parameters are usually passed as a tuple. If you want to make a tuple with one element, the syntax is

    val = ('test',)
    

    If you miss the comma, then val will just hold the string 'test', which won't work as your statement parameters.