在 Python 中插入数据库时出错
Error when inserting into Database in Python
产生此错误的代码有什么问题
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
我在 XAMPP 中使用 XAMPP 本地主机数据库和 mysql.connector 包 Python。我的插入代码:
cursor = db.cursor()
username = input()
sql = "INSERT INTO `employee` (`id`, `username`) VALUES (NULL, %s)"
cursor.execute(sql, username)
db.commit()
print("success")
代替这一行
cursor.execute(sql, username)
试试这个
cursor.execute(sql, [username])
execute 需要一个列表/(元组列表)作为第二个参数。如果您需要更多详细信息,可以查看 documentation。
产生此错误的代码有什么问题
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
我在 XAMPP 中使用 XAMPP 本地主机数据库和 mysql.connector 包 Python。我的插入代码:
cursor = db.cursor()
username = input()
sql = "INSERT INTO `employee` (`id`, `username`) VALUES (NULL, %s)"
cursor.execute(sql, username)
db.commit()
print("success")
代替这一行
cursor.execute(sql, username)
试试这个
cursor.execute(sql, [username])
execute 需要一个列表/(元组列表)作为第二个参数。如果您需要更多详细信息,可以查看 documentation。