Python psycopg2查询参数类型
我有一个python脚本,它使用psycopg2调用postgresql过程。proc具有以下签名Python psycopg2查询参数类型,python,postgresql,psycopg2,Python,Postgresql,Psycopg2,我有一个python脚本,它使用psycopg2调用postgresql过程。proc具有以下签名 CREATE FUNCTION utility_function(p_id_file bigint, p_size bigint, p_id_volume smallint, p_id_type_file bigint, p_id_user bigint) RETURNS void 当我用下面的代码调用它时,我得到了这个错误 Traceback (most recent call last):
CREATE FUNCTION utility_function(p_id_file bigint, p_size bigint, p_id_volume smallint, p_id_type_file bigint, p_id_user bigint) RETURNS void
当我用下面的代码调用它时,我得到了这个错误
Traceback (most recent call last):
File "/tmp/regenerate/regenerate.py", line 173, in <module>
execute_process(db, out_csv, out_file, start_date, end_date, limit, offset)
File "/tmp/regenerate/regenerate.py", line 57, in execute_process
db.execute(sql, (id_file, size, id_volume, id_type_file, id_user))
psycopg2.ProgrammingError: function utility_function(integer, integer, integer, integer, integer) does not exist
LINE 1: select * from utility_function(13456, 132456798, 0...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
db.mogrify返回以下内容:
select * from utility_regenerate_tsstream(13456, 132456798, 0, 42, 1324)
提前感谢您的帮助:)
Sam我通过将proc的参数类型从smallint更改为int,成功地解决了这个问题。似乎psycopg2无法处理smallint的类型推断 p_id_volume smallint->p_id_volume int
不需要其他更改。您可以将查询中的类型指定为强制转换:
sql = 'select * from utility_function(%s::bigint, %s::bigint, %s::smallint, %s::bigint, %s::bigint)'
张贴代码中为
id_file
、size
等赋值的部分。它们是从另一个查询中赋值的,比如:db.execute(sql)id_file=db[0],等等。Psycopg无法对查询的目标进行类型推断:它使用python对象的类型进行sql表示,python中没有smallint。
sql = 'select * from utility_function(%s::bigint, %s::bigint, %s::smallint, %s::bigint, %s::bigint)'