2012-03-26 7 views
2

psycopg2を使用してpostgresql procを呼び出すpythonスクリプトがあります。 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) 

感謝:) サム

+0

ポストあなたは 'id_file'、' size'に値を代入し、あなたのコードの一部など –

+0

は、彼らは別のクエリから割り当てられています、 – Nostradamnit

答えて

1

私はPROCのパラメータの型を変更することで問題を克服するために管理してきましたsmallintからintへpsycopg2がSMALLINTのための型推論を...処理しないと思われ

p_id_volume SMALLINT - > p_id_volume int型

その他の変更が必要とされませんでした。キャストとしてクエリで型を指定することができます

+2

Psycopgはクエリのターゲットに対して型推論を行うことはできません:これはPythonオブジェクトの型を使用してSQL表現を作成しますが、それは存在しません(例:db.execute(sql)id_file = db [0] Pythonではsmallintです。 – piro

4

sql = 'select * from utility_function(%s::bigint, %s::bigint, %s::smallint, %s::bigint, %s::bigint)' 
関連する問題