摘要:在本教程中,您将学习如何使用 MySQL Connector/Python API 在 Python 中调用 MySQL 存储过程。
在我们开始之前
如果您不熟悉 MySQL 存储过程或想回顾一下它作为复习,您可以按照MySQL 存储过程教程进行操作。
我们将创建两个存储过程用于本教程中的演示。
以下存储过程返回books
和authors
表中包含作者信息的所有书籍:
USE python_mysql;
DELIMITER $$
CREATE PROCEDURE find_all()
BEGIN
SELECT
title,
isbn,
CONCAT(first_name,' ',last_name) AS author
FROM books
INNER JOIN book_author
ON book_author.book_id = books.id
INNER JOIN authors
ON book_author.author_id = authors.id
ORDER BY title;
END$$
DELIMITER ;
Code language: SQL (Structured Query Language) (sql)
find_all()
存储过程有一个带有INNER JOIN
子句的SELECT
语句,用于从books
和authors
表中检索书名、ISBN 和作者的全名。
调用find_all()
存储过程,您将得到以下结果集:
CALL find_all();
Code language: SQL (Structured Query Language) (sql)
第二个名为find_by_isbn()
的存储过程用于按 ISBN 查找书籍,如下所示:
DELIMITER $$
CREATE PROCEDURE find_by_isbn(
IN p_isbn VARCHAR(13),
OUT p_title VARCHAR(255)
)
BEGIN
SELECT title
INTO p_title
FROM books
WHERE isbn = p_isbn;
END$$
DELIMITER ;
Code language: SQL (Structured Query Language) (sql)
find_by_isbn()
接受两个参数:第一个参数是 ISBN(IN 参数),第二个参数是标题(OUT 参数)。当您将 ISBN 传递给存储过程时,您将获得书名,例如:
CALL find_by_isbn('1235927658929',@title);
SELECT @title;
Code language: SQL (Structured Query Language) (sql)
这是输出:
+-------------------------------------------------+
| @title |
+-------------------------------------------------+
| Debatable Land Between This World and the Next |
+-------------------------------------------------+
1 row in set (0.00 sec)
Code language: SQL (Structured Query Language) (sql)
从 Python 调用存储过程
要在 Python 中调用存储过程,请按照以下步骤操作:
- 通过创建新的
MySQLConnection
对象连接到数据库。 - 通过调用
cursor()
方法从MySQLConnection
对象实例化一个新的MySQLCursor
对象。 - 调用
MySQLCursor
对象的callproc()
方法。您将存储过程的名称作为callproc()
方法的第一个参数传递。如果存储过程需要参数,则需要将列表作为第二个参数传递给callproc()
方法。如果存储过程返回结果集,您可以调用MySQLCursor
对象的stored_results()
方法来获取列表迭代器,并使用fetchall()
方法迭代该结果集。 - 一如既往地关闭游标和数据库连接。
以下示例演示如何在Python中调用find_all()
存储过程并输出结果集。
from mysql.connector import MySQLConnection, Error
from python_mysql_dbconfig import read_db_config
def call_find_all_sp():
try:
db_config = read_db_config()
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
cursor.callproc('find_all')
# print out the result
for result in cursor.stored_results():
print(result.fetchall())
except Error as e:
print(e)
finally:
cursor.close()
conn.close()
if __name__ == '__main__':
call_find_all_sp()
Code language: Python (python)
以下示例显示如何调用find_by_isbn()
存储过程。
from mysql.connector import MySQLConnection, Error
from python_mysql_dbconfig import read_db_config
def call_find_by_isbn():
try:
db_config = read_db_config()
conn = MySQLConnection(**db_config)
cursor = conn.cursor()
args = ['1236400967773', 0]
result_args = cursor.callproc('find_by_isbn', args)
print(result_args[1])
except Error as e:
print(e)
finally:
cursor.close()
conn.close()
if __name__ == '__main__':
call_find_by_isbn()
Code language: Python (python)
find_by_isbn()
存储过程需要两个参数,因此,我们必须传递一个包含两个元素的列表 ( args
):第一个是 isbn (1236400967773),第二个是 0。args 列表的第二个元素 (0)只是保存p_title
参数的占位符。
callproc()
方法返回一个包含两个元素的列表 ( result_args
):第二个元素 (result_args[1]) 保存p_title
参数的值。
在本教程中,您学习了如何使用MySQLCursor
对象的callproc()
方法在 Python 中调用存储过程。