简体   繁体   中英

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:

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. The table has more columns(but these are the relevant ones) and is generated using 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, ? , for parameter placeholders similar to most JDBC connections.

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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