2012-03-26 5 views
2

У меня есть скрипт python, который вызывает proc postgresql, используя psycopg2. У proc есть следующая подпись.Параметры параметров запроса 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) 

Заранее спасибо за вашу помощь :) Sam

+0

сообщение часть кода, где вы назначаете значения '' id_file', size' и т.д. –

+0

Они назначаются из другого запроса, например: db.execute (sql) id_file = db [0] и т. д. – Nostradamnit

ответ

1

мне удалось преодолеть эту проблему путем изменения типа параметра ргос от smallint до int. Кажется, что psycopg2 не обрабатывает вывод типа для SMALLINT ...

p_id_volume SMALLINT -> p_id_volume INT

не были нужны никакие другие изменения.

+2

Psycopg не может делать вывод типа в целевом объекте запроса: он использует тип объекта python для представления sql, и нет smallint в Python. – piro

4

Вы можете указать типы в запросе, как бросает:

sql = 'select * from utility_function(%s::bigint, %s::bigint, %s::smallint, %s::bigint, %s::bigint)' 
Смежные вопросы