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

相关文章:

  • 2021-12-16
  • 2021-08-01
  • 2021-06-27
  • 2021-07-28
  • 2021-08-25
  • 2021-06-21
  • 2022-12-23
  • 2022-01-09
猜你喜欢
  • 2022-12-23
  • 2022-01-21
  • 2022-12-23
  • 2021-06-04
  • 2021-10-19
  • 2021-06-29
  • 2021-09-10
相关资源
相似解决方案