【问题标题】:mariadb.ProgrammingError: SQL syntax error INSERT INTO issue with (%s/?)mariadb.ProgrammingError:SQL 语法错误 INSERT INTO 问题与 (%s/?)
【发布时间】:2025-12-17 13:00:01
【问题描述】:

我正在使用 python 3.9 我的 sql 服务器在 10.3.29-MariaDB-0+deb10u1 Raspbian 10

我想使用 python 在我的 sql 数据库的新行中插入一个字段。

# python3!
import mariadb

mydb = mariadb.connect(
    host="***(just wanted to exclude the domain)",
    port=25555,
    user="test",
    password="test",
    database="test"
)
cursor = mydb.cursor()

first = "test5"
cursor.execute("INSERT INTO contacts (firstname) VALUES (%s)",
               first)

mydb.commit()
cursor.close()
mydb.close()

我收到此错误:

Traceback (most recent call last):
  File "E:\contact_test\contacts\tests.py", line 14, in <module>
    cursor.execute("INSERT INTO contacts (firstname) VALUES (%s)",
mariadb.ProgrammingError: 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

我尝试在 (%s) 和 (?) 之间进行更改,但没有任何区别。

当我尝试直接在数据库中使用相同的命令时:

MariaDB [test]> INSERT INTO contacts (firstname) VALUES ("test5");
Query OK, 1 row affected (0.009 sec)

我变成了这个输出:

MariaDB [test]> SELECT * FROM contacts;
+----+-----------+------------+---------+----------+----------+--------------+--------------+-------+
| id | firstname | secondname | sirname | landline | mobile   | mailaddress1 | mailaddress2 | group |
+----+-----------+------------+---------+----------+----------+--------------+--------------+-------+
|  . | ...       |            |         |          |          |              |              |     . |
|  6 | test5     |            |         |          |          |              |              |     0 |
+----+-----------+------------+---------+----------+----------+--------------+--------------+-------+
5 rows in set (0.001 sec)

所以它起作用了。



更新:

我也有以下变化:

sql = "INSERT INTO contacts (firstname) VALUES (%s)"
val = ("test5")

cursor.execute(sql, val)

输出:

Traceback (most recent call last):
  File "E:\coding\contact\contacts_test\v1.0\test.py", line 14, in <module>
    cursor.execute("INSERT INTO contacts (firstname) VALUES (%s)",
mariadb.ProgrammingError: 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

和:

first = "test5"

cursor.execute("INSERT INTO contacts (firstname) VALUES (%s)",
               (first))

输出:

Traceback (most recent call last):
  File "E:\coding\contact\contacts_test\v1.0\test.py", line 15, in <module>
    cursor.execute("INSERT INTO contacts (firstname) VALUES (%s)",
mariadb.ProgrammingError: 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

【问题讨论】:

  • 认为first 需要是一个元组
  • val = ("test5") --> val = ("test5",)
  • @balderman 谢谢解决了我的问题

标签: python mysql mariadb


【解决方案1】:

已解决:

按照@balderman 的建议,我添加了 {,}:

first = "test7"
cursor.execute("INSERT INTO contacts (firstname) VALUES (%s)",
               (first,))

Python 输出:

Process finished with exit code 0

sql输出:

MariaDB [test]> SELECT * FROM contacts WHERE firstname = 'test7';
+----+-----------+------------+---------+----------+--------+--------------+--------------+-------+
| id | firstname | secondname | sirname | landline | mobile | mailaddress1 | mailaddress2 | group |
+----+-----------+------------+---------+----------+--------+--------------+--------------+-------+
|  8 | test7     |            |         |          |        |              |              |     0 |
+----+-----------+------------+---------+----------+--------+--------------+--------------+-------+
1 row in set (0.001 sec)

这样我的问题就解决了。感谢所有的帮助! :D

【讨论】: