Example of using python to connect to mysql to call a stored procedure

Source: Internet
Author: User
This article mainly introduces the example of using python to connect to mysql to call the stored procedure. For more information, see The code is as follows:


#! /Usr/bin/env python
#-*-Coding: utf8 -*-
Import MySQLdb
Import time
Import OS, sys, string
Def CallProc (id, onlinetime ):
''' Call the stored procedure,
Input parameter: Number, online time, output: Account, password;
Use the output parameter method '''
Accname =''
Accpwd =''
Conn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = '000000', db = 'ceshi ')
Cur = conn. cursor ()
Cur. callproc ('proctest', (id, onlinetime, accname, accpwd ))
Cur.exe cute ('select @ _ proctest_2, @ _ proctest_3 ')
Data = cur. fetchall ()
If data:
For rec in data:
Accname = rec [0]
Accpwd = rec [1]
Cur. close ()
Conn. close ();
Return accname, accpwd
Def CallProct (id, onlinetime ):
''' Call the stored procedure,
Input parameter: Number, online time, output: Account, password;
Use the select return record method '''
Accname =''
Accpwd =''
Conn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = '000000', db = 'ceshi ')
Cur = conn. cursor ()
Cur. nextset ()
Cur.exe cute ('Call ptest (% s, % s) ', (id, onlinetime ))
Data = cur. fetchall ()
If data:
For rec in data:
Accname = rec [0]
Accpwd = rec [1]
Cur. close ()
Conn. close ();
Return accname, accpwd
Name, pwd = CallProct (1, 0)
Print name, pwd

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.