python3操作pgsql報錯:No operator matches the given name and argument type(s),You might need to add explicit type casts


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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM