首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 數據庫知識 >  >> MySQL 資料庫 >> MySQL 綜合教學

Python 呼叫 MySQL 預存程序取得 OUT 參數完整實作指南

     編輯:MySQL 綜合教學

Python 呼叫 MySQL 預存程序取得 OUT 參數完整實作指南

MySQLdb / PyMySQL 呼叫含 OUT/INOUT 參數的預存程序時,參數值不會直接回傳於 callproc() 結果中,需額外執行 SELECT @_procname_n 讀取伺服器端的會話變數。本文說明完整操作流程與程式碼範例。

核心機制

  • 呼叫 cursor.callproc('proc_name', (arg1, arg2, ...)) 後,MySQL 會將參數存為會話變數:@_proc_name_0, @_proc_name_1...(索引從 0 開始)
  • OUT/INOUT 參數的修改值需透過 SELECT @_proc_name_n 取得
  • 呼叫過程會產生一個空結果集,需用 nextset() 推進(Connector/Python 自動處理,MySQLdb 需手動)

實際專案程式碼範例

import MySQLdb

conn = MySQLdb.connect(host='localhost', user='root', passwd='pass', db='mydb')
cursor = conn.cursor()

# 呼叫含 OUT 參數的預存程序
# DeleteUser(user_id, OUT error_msg)
user_id = 'user123'
out_error = ''  # 佔位參數
cursor.callproc("DeleteUser", (user_id, out_error))

# 取得 OUT 參數(第 2 個參數,索引 1)
cursor.execute("SELECT @_DeleteUser_1")
result = cursor.fetchall()

if result and result[0][0]:  # 非 NULL 表示有錯誤
    print("Not Found:", user_id, "\t", result[0][0])
else:
    print("Deleted:", user_id)

# 若有多個 OUT 參數,依序查詢
# cursor.execute("SELECT @_DeleteUser_2")
# cursor.execute("SELECT @_DeleteUser_3")
# result = cursor.fetchall()

cursor.close()
conn.close()

完整流程步驟

  1. 準備輸入參數與 OUT 參數佔位值
  2. 呼叫 cursor.callproc('程序名', 參數元組)
  3. 對每個 OUT 參數執行 cursor.execute("SELECT @_程序名_索引")
  4. 用 fetchall() 或 fetchone() 取得結果(回傳為 tuple,取 [0][0] 為實際值)
  5. 根據值執行後續邏輯

注意事項

參數索引從 0 開始: 第 1 個參數對應 @_proc_0,第 2 個對應 @_proc_1,以此類推。
僅支援 MySQL 5.0+: 預存程序功能需 MySQL 5.0 以上版本。
PEP 249 差異: 標準規定 OUT 參數應直接回傳於 callproc 結果,但 MySQL 因協定限制無法一致實現,故需額外 SELECT。

進階:封裝為通用函數

def call_proc_with_out(cursor, proc_name, args, out_indices):
    """
    args: 完整參數列表(含 OUT 佔位)
    out_indices: 需取回的 OUT 參數索引列表,如 [1, 2]
    回傳: (結果列表, out參數字典)
    """
    cursor.callproc(proc_name, args)
    out_values = {}
    for idx in out_indices:
        cursor.execute(f"SELECT @_{proc_name}_{idx}")
        row = cursor.fetchone()
        out_values[idx] = row[0] if row else None
    # 取得結果集(如有)
    results = []
    for result_cursor in cursor.stored_results():
        results.extend(result_cursor.fetchall())
    return results, out_values

# 使用範例
results, outs = call_proc_with_out(cursor, 'DeleteUser', ('user123', '', ''), [1, 2])
print("OUT參數:", outs)

掌握此模式,即可在 Python 中完整處理 MySQL 預存程序的輸入輸出參數與結果集。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved