簡體   English   中英

psycopg2查詢參數類型

[英]psycopg2 query parameter types

我有一個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):
  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

您可以將查詢中的類型指定為強制轉換:

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

我已經設法通過將proc的參數類型從smallint更改為int來克服該問題。 似乎psycopg2不處理smallint的類型推斷...

p_id_volume smallint - > p_id_volume int

無需其他更改。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM