How does Python call MySQL stored procedures?

Source: Internet
Author: User

This article mainly describes the correct operation steps for calling the MySQL stored procedure using Python, as well as the matters worth your attention in actual operations, if you are interested, you can click the following articles to view them.

Environment:

1. MySQL or later versions that support MySQL stored procedures

2. Install MySQL-python, which currently supports 2.x

Steps:

I. Database preparation

1. Create a table

 
 
  1. view sourceprint?1 CREATE TABLE `Account` (   
  2. `id` BIGINT(20) NOT NULL AUTO_INCREMENT,   
  3. `sm_accountName` VARCHAR(100) COLLATE gbk_chinese_ci NOT NULL DEFAULT '',   
  4. `sm_password` TEXT COLLATE gbk_chinese_ci NOT NULL,   
  5. `sm_onlineTime` BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',   
  6. PRIMARY KEY (`id`),   
  7. UNIQUE KEY `accountNameIndex` (`sm_accountName`)   
  8. )ENGINE=InnoDB   

2. Create a MySQL Stored Procedure

 
 
  1. view sourceprint?01 CREATE PROCEDURE `proctest`(IN i_id BIGINT, IN i_onlinetime BIGINT, OUT o_accname VARCHAR(30), OUT o_accpwd VARCHAR(50))   
  2. 02 NOT DETERMINISTIC   
  3. 03 CONTAINS SQL   
  4. 04 SQL SECURITY DEFINER   
  5. 05 COMMENT ''   
  6. 06 BEGIN   
  7. 07 select sm_accountName,sm_password   
  8. 08 into o_accname,o_accpwd   
  9. 09 from `tbl_Account` where id=i_id and sm_onlineTime=i_onlinetime limit 1;   
  10. 10 END;   

3. insert part of data

 
 
  1. view sourceprint?1 INSERT INTO `Account` (`id`, `sm_accountName`, `sm_password`, `sm_onlineTime`) VALUES   
  2. 2 (1, 'luoshulin', 'asdfsdf', 0),   
  3. 3 (2, 'test', '1', 0),   
  4. 4 (3, 'adsfasd', 'asdf', 1);   

The database-related content is ready. Next, write the python script.

Ii. python script

 
 
  1. view sourceprint?01 #!/usr/bin/env python   
  2. 02 # -*- coding: utf8 -*-   
  3. 03 import MySQLdb   
  4. 04 import time   
  5. 05 import os, sys, string   
  6. 06 def CallProc(id,onlinetime):   

07 ''' call the MySQL stored procedure,

08 input parameter: Number, online time, output: account, password;

09 use the output parameter method '''

10 accname =''

11 accpwd =''

12 conn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = '000000', db = 'ceshi ')

13 cur = conn. cursor ()

14 cur. callproc ('proctest', (id, onlinetime, accname, accpwd ))

15 cur.exe cute ('select @ _ proctest_2, @ _ proctest_3 ')

16 data = cur. fetchall ()

17 if data:

18 for rec in data:

19 accname = rec [0]

20 accpwd = rec [1]

21 cur. close ()

22 conn. close ();

23 return accname, accpwd

24 def CallProct (id, onlinetime ):

25 ''' call the MySQL stored procedure,

26 input parameter: Number, online time, output: account, password;

27 use the select return record method '''

28 accname =''

29 accpwd =''

30 conn = MySQLdb. connect (host = 'localhost', user = 'root', passwd = '000000', db = 'ceshi ')

31 cur = conn. cursor ()

32 cur. nextset ()

33 cur.exe cute ('call ptest (% s, % s) ', (id, onlinetime ))

34 data = cur. fetchall ()

35 if data:

36 for rec in data:

37 accname = rec [0]

38 accpwd = rec [1]

39 cur. close ()

40 conn. close ();

41 return accname, accpwd

42 name, pwd = CallProct (1, 0)

43 print name, pwd

Iii. Test

Save the python script and execute it to view the result.

View sourceprint? 1 [root @ redhat-dev python] # python pycallproc. py

2 luoshulin asdfsdf

The test uses the select return record method. The results returned by the output parameters are the same.

The above content is an introduction to calling the MySQL stored procedure in python. I hope you will get some benefits.

Related Article

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.