【问题标题】:Error when inserting into Database in Python在 Python 中插入数据库时​​出错
【发布时间】:2019-08-24 19:42:41
【问题描述】:

产生此错误的代码有什么问题

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

我在 Python 中使用 XAMPP 本地主机数据库和 mysql.connector 包。我的插入代码:

cursor = db.cursor()
username = input()
sql = "INSERT INTO `employee` (`id`, `username`) VALUES (NULL, %s)"
cursor.execute(sql, username)
db.commit()
print("success")

【问题讨论】:

    标签: python-3.6


    【解决方案1】:

    代替这一行

    cursor.execute(sql, username)
    

    试试这个

    cursor.execute(sql, [username])
    

    execute 需要一个列表/(元组列表)作为第二个参数。如果您需要更多详细信息,可以查看documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-26
      • 2016-05-23
      • 2016-10-25
      • 1970-01-01
      • 2019-03-04
      • 1970-01-01
      • 1970-01-01
      • 2016-06-06
      相关资源
      最近更新 更多