2012-03-26 108 views
2

我有使用psycopg2调用一个PostgreSQL PROC python脚本。该过程具有以下签名。psycopg2查询参数类型

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. 

我无法弄清楚如何将参数转换为正确的PostgreSQL类型。

的Python代码看起来是这样的。

sql = 'select * from utility_function(%s, %s, %s, %s, %s)' 
logging.debug(db.mogrify(sql, (id_file, size, id_volume, id_type_file, id_user))) 
db.execute(sql, (id_file, size, id_volume, id_type_file, id_user)) 

db.mogrify返回此:

select * from utility_regenerate_tsstream(13456, 132456798, 0, 42, 1324) 

在此先感谢您的帮助:) 山姆

+0

发布您的代码,你赋值'id_file','size'等 – 2012-03-26 11:12:34

+0

他们是从另一个查询分配的一部分,如:db.execute(sql)id_file = db [0]等... – Nostradamnit 2012-03-26 11:56:11

回答

1

我已经设法通过改变PROC的参数类型克服的问题从smallint到int。看来,psycopg2不办理SMALLINT类型推理...

p_id_volume SMALLINT - > p_id_volume INT

需要有没有其他的变化。

+2

Psycopg无法对查询的目标进行类型推断:它使用python对象的类型来生成sql表示,并且没有Python中的smallint。 – piro 2012-03-29 20:12:37

4

您可以在查询中指定类型的转换:

sql = 'select * from utility_function(%s::bigint, %s::bigint, %s::smallint, %s::bigint, %s::bigint)'