You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MariaDB 10.3中替代MySQL的cursor.callproc调用存储过程?

解决MariaDB 10.3中Python cursor.callproc未定义的问题

1. 更换为MariaDB官方Python驱动

mysql-connector-python对旧版MariaDB的兼容性有限,建议替换为MariaDB官方驱动mariadb-connector-python,它对MariaDB特性的支持更完善。
安装命令:

pip install mariadb-connector-python

更换驱动后的调用示例:

import mariadb

# 建立数据库连接
conn = mariadb.connect(
    user="你的用户名",
    password="你的密码",
    host="localhost",
    database="目标数据库"
)

# 创建游标
cursor = conn.cursor()

# 调用存储过程(示例:存储过程名为my_proc,传入两个参数)
cursor.callproc("my_proc", (1, "测试参数"))

# 读取存储过程返回的结果集
for result_set in cursor.stored_results():
    print(result_set.fetchall())

# 关闭资源
cursor.close()
conn.close()

2. 直接使用SQL CALL语句替代callproc方法

如果不想更换驱动,可以通过cursor.execute直接执行CALL语句调用存储过程:

# 假设使用mysql-connector-python连接
import mysql.connector

conn = mysql.connector.connect(
    user="你的用户名",
    password="你的密码",
    host="localhost",
    database="目标数据库"
)
cursor = conn.cursor()

# 执行CALL语句传入参数
cursor.execute("CALL my_proc(%s, %s)", (1, "测试参数"))

# 获取结果(若有多个结果集,需循环读取)
results = cursor.fetchall()
while cursor.nextset():
    results.extend(cursor.fetchall())

print(results)

cursor.close()
conn.close()

说明

MariaDB 10.3本身支持存储过程,问题根源在于驱动兼容性:mysql-connector-python部分版本未针对MariaDB实现callproc方法,而官方驱动则完全适配MariaDB的存储过程调用逻辑。

内容的提问来源于stack exchange,提问作者awerner

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 19:45:35