时间:2021-07-01 10:21:17 帮助过:4人阅读
The find_all()
stored procedure has a SELECT statement with JOIN clauses that retrieve title, isbn and author’s full name from books
and authors
tables. When we execute the find_all()
stored procedure, it returns a result as follows:
1 | CALL find_all(); |
The second stored procedure named find_by_isbn()
that is used to find a book by its ISBN as follows:
1 2 3 4 5 6 7 8 9 | 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 ; |
The find_by_isbn()
accepts two parameters: the first parameter is isbn (IN parameter) and second is title (OUT parameter). When you pass the isbn to the stored procedure, you will get the title of the book, for example:
1 2 | CALL find_by_isbn(‘1235927658929‘,@title); SELECT @title; |
To call a stored procedure in Python, you follow the steps below:
callproc()
method of the MySQLCursor object. You pass the stored procedure’s name as the first argument of the callproc()
method. If the stored procedure requires parameters, you need to pass a list as the second argument to the callproc()
method. In case the stored procedure returns a result set, you can invoke the stored_results()
method of the MySQLCursor object to get a list iterator and iterate this result set by using the fetchall()
method.The following example demonstrates how to call the find_all()
stored procedure in Python and output the result set.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | 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() |
The following example shows you how to call the find_by_isbn()
stored procedure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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() |
The find_by_isbn()
stored procedure requires two parameters therefore we have to pass a list ( args
) that contains two elements: the first one is isbn (1236400967773) and the second is 0. The second element of the args list (0) is just a placeholder to hold the p_title
parameter.
The callproc()
method returns a list ( result_args
) that contains two elements: the second element (result_args[1]) holds the value of the p_title
parameter.
In this tutorial, we have shown you how to call stored procedures in Python by using callproc()
method of the MySQLCursor object.
Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python
标签: