簡體   English   中英

Python psycopg2 PostgreSQL嘗試取消分配

[英]Python psycopg2 PostgreSQL attemp to deallocate

我想知道是否有可能在嘗試打開PostgreSQL准備好的語句之前將其釋放

目前,我正在使用以下代碼來取消分配所有准備好的語句

cur_deallocate = gv_pgsql.cursor()
cur_deallocate.execute("deallocate all")

cur_users = gv_pgsql.cursor()
cur_users.execute("prepare cur_users as " +
                  "select * from users")

這是可行的,但是如果在單獨的函數中還有我需要的另一個准備好的語句,那么它也會被關閉。 但是,如果我使用deallocate cur_users,則該程序將崩潰,並顯示以下錯誤:無法取消分配不存在的已准備好的語句。

有沒有解決的辦法? 嘗試解除分配,如果失敗,則准備語句

在except塊中捕獲該錯誤,或​​者使用pg_prepared_statements視圖查看該錯誤是否首先存在: 在此處輸入鏈接描述在我們的安裝語句中名稱以小寫字母存儲

def safe_deallocate(self, statement_name):
    curs.execute(
        "select true from pg_prepared_statements where name = lower(%s)", (statement_name,))
    if curs.rowcount:
        curs.execute("deallocate {}".format(statement_name))

暫無
暫無

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

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