簡體   English   中英

MySQL python 連接器更新錯誤:檢查正確的語法以在 %s 附近使用。 出了什么問題?

[英]MySQL python connector UPDATE error: check right syntax to use near %s. What is going wrong?

這應該相對快速和容易:

當我在我的 python IDE 中運行它時

mycursor.executemany("UPDATE table42 SET date = %s ", [('2020-05-11')])

由於某種原因,它在字符串占位符 (%s) 處完全被絆倒了。 我使用 executemany 的原因是因為很快該字符串將被使用today.strftime('%Y-%m-%d')的變量替換,所以我需要它更靈活。

錯誤如下: 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 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

如果有人可以幫助我找出我做錯了什么,將不勝感激。

在日期字符串之后需要有一個逗號,以便列表是一個元組列表(創建元組的是逗號,而不是括號)。

mycursor.executemany("UPDATE table42 SET date = %s ", [('2020-05-11',)])

這是因為DB API需要該參數

作為序列或映射提供

(從技術上講,字符串是一個序列,但在這里它的長度是錯誤的。無論如何我認為 mysql-connector 拒絕一個字符串,pymysql 會在這個 IIRC 中接受一個字符串。但是將參數設為元組絕對是最便攜的方式編碼這個)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM