简体   繁体   English

SQL语句中的语法错误:适用于Python 3.6的UPDATE

[英]Syntax error in SQL statement: UPDATE for Python 3.6

I am attempting to update the column keys of the book table of an H2 database (via JayDeBeApi) using this command: 我正在尝试使用以下命令来更新H2数据库的书本表的列 (通过JayDeBeApi):

sqlCommand = "UPDATE book SET keys = %s WHERE keys IS NULL AND id = %d"
val = (keyWords, idBook)
mydb.execute(sqlCommand, val)

keyWords is of type string and idBook is of type int. keyWords是字符串类型,而idBook是int类型。 The table has more columns(but these are the relevant ones) and is generated using Spring-Boot: 该表具有更多列(但这些是相关的列),并使用Spring-Boot生成:

@Id
private Integer id;

@Column(columnDefinition="varchar(5000)")
private String keys;

But I am getting this error: 但我收到此错误:

Traceback (most recent call last):
  File "init.py", line 43, in <module>
    sql.execute(sqlCommand, val)
  File "C:\Users\Us\AppData\Local\Programs\Python\Python36-32\lib\site-packages\jaydebeapi\__init__.py", line 498, in execute
    self._prep = self._connection.jconn.prepareStatement(operation)
jpype._jexception.JdbcSQLExceptionPyRaisable: org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "UPDATE BOOK SET KEYS = %[*]S WHERE KEYS IS NULL AND ID = %D "; expected "DEFAULT, NOT, EXISTS, INTERSECTS, SELECT, FROM, WITH"; SQL statement:
UPDATE book SET keys = %s WHERE keys IS NULL AND id = %d [42001-197]

I see that the problem is caused by the formatting types, but I have absolutely no idea how to fix it. 我看到问题是由格式类型引起的,但是我绝对不知道如何解决它。

Any help is appreciated. 任何帮助表示赞赏。

From docs (actually a bug issue to adhere to Python's PEP 249 standard), it appears the JayDeBe API uses qmarks, ? 从文档(实际上是一个错误的问题要坚持Python的PEP 249标准),它出现在JayDeBe API使用qmarks, ? , for parameter placeholders similar to most JDBC connections. ,用于类似于大多数JDBC连接的参数占位符。

Thus, you must use these placeholders in your prepared statement for all parameter values without differentiating by types (ie, number, string, date). 因此,必须在准备好的语句中为所有参数值使用这些占位符,而不能按类型(即数字,字符串,日期)进行区分。 The database takes care of type conversion. 数据库负责类型转换。

sqlCommand = "UPDATE book SET keys = ? WHERE keys IS NULL AND id = ?"
val = (keyWords, idBook)

mydb.execute(sqlCommand, val)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM