zoukankan      html  css  js  c++  java
  • Home / Python MySQL Tutorial / Calling MySQL Stored Procedures in Python Calling MySQL Stored Procedures in Python

    f you are not familiar with MySQL stored procedures or want to review it as a refresher, you can follow the MySQL stored procedures tutorial.

    We will create two stored procedures for the demonstration in this tutorial. The first stored procedure gets all books with authors information from  books and  authors tables:

    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:

    Python MySQL Stored Procedure Example

    The second stored procedure named  find_by_isbn() that is used to find a book by its ISBN as follows:

    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:

    Calling stored procedures from Python

    To call a stored procedure in Python, you follow the steps below:

    1. Connect to MySQL database by creating a new MySQLConnection object.
    2. Instantiate a new MySQLCursor object from the MySQLConnection object by calling the cursor() method.
    3. Call  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.
    4. Close the cursor and database connection as always.

    The following example demonstrates how to call the  find_all() stored procedure in Python and output the result set.

    The following example shows you how to call the  find_by_isbn() stored procedure.

    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.

  • 相关阅读:
    事件处理之二:点击事件监听器的五种写法 分类: H1_ANDROID 2013-09-11 10:32 4262人阅读 评论(1) 收藏
    如何解决安卓SDK无法下载Package的问题 分类: H1_ANDROID 2013-09-09 10:26 1199人阅读 评论(0) 收藏
    adb常用命令 分类: H1_ANDROID 2013-09-08 15:22 510人阅读 评论(0) 收藏
    用IBM WebSphere DataStage进行数据整合: 第 1 部分 分类: H2_ORACLE 2013-08-23 11:20 688人阅读 评论(0) 收藏
    三大主流ETL工具选型 分类: H2_ORACLE 2013-08-23 11:17 426人阅读 评论(0) 收藏
    ETL概述 分类: H2_ORACLE 2013-08-23 10:36 344人阅读 评论(0) 收藏
    POI操作Excel常用方法总结 分类: B1_JAVA 2013-08-23 10:01 349人阅读 评论(0) 收藏
    段的创建表user_segments 分类: H2_ORACLE 2013-08-10 11:13 714人阅读 评论(0) 收藏
    让android项目支持boost 支持c++11
    unity中全屏背景图缩放
  • 原文地址:https://www.cnblogs.com/kungfupanda/p/5937326.html
Copyright © 2011-2022 走看看