python操作數據庫時報錯了,報錯內容為“No operator matches the given name and argument type(s),You might need to add explicit type casts”,原因在於字段格式不正確。
舉例:
import psycopg2 code='123' # 建立連接 conn=psycopg2.connect(database="",user="",password="",host="",port="") # 游標 cur=conn.cursor() # query query=("select ... where code = %s" %code) # 執行query cur.execute(query) # 獲取結果 rows=cur.fetchall() print(rows)
運行結果:報錯,提示"No operator matches the given name and argument type(s),You might need to add explicit type casts"
修改query為query=("select ... where code = cast (%s as VARCHAR)" %code)
運行結果:查詢成功
報錯原因在於字段格式錯誤,可通過cast(字段 as VARCHAR)指定格式為VARCHAR。
本文轉載https://blog.csdn.net/yongshiaoteman/article/details/81095864