python连接mysql调用存储过程示例

yipeiwu_com6年前Python基础

复制代码 代码如下:

#!/usr/bin/env python
# -*- coding: utf8 -*-
import MySQLdb
import time
import os, sys, string
def CallProc(id,onlinetime):
'''调用存储过程,
输入参数:编号,在线时间,输出:帐号,密码;
使用输出参数方式'''
accname=''
accpwd=''
conn = MySQLdb.connect(host='localhost',user='root',passwd='111111',db='ceshi')
cur =conn.cursor()
cur.callproc('proctest',(id,onlinetime,accname,accpwd))
cur.execute('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):
'''调用存储过程,
输入参数:编号,在线时间,输出:帐号,密码;
使用select返回记录方式'''
accname=''
accpwd=''
conn = MySQLdb.connect(host='localhost',user='root',passwd='111111',db='ceshi')
cur =conn.cursor()
cur.nextset()
cur.execute('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

相关文章

python dataframe NaN处理方式

将dataframe中的NaN替换成希望的值 import pandas as pd df1 = pd.DataFrame([{'col1':'a', 'col2':1}, {'co...

Python制作词云图代码实例

Python制作词云图代码实例

词云图是将词汇按照频率的高低显示不同大小而形成的图,可以一目了然地看出关键词。下面是词云图的python代码~ #导入需要模块 import jieba import numpy a...

朴素贝叶斯Python实例及解析

本文实例为大家分享了Python朴素贝叶斯实例代码,供大家参考,具体内容如下 #-*- coding: utf-8 -*- #添加中文注释 from numpy import *...

Python字符串的修改方法实例

这篇文章主要介绍了Python字符串的修改方法实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 我们在修改字符串时 通常遇到报错:...

基于Python函数和变量名解析

基于Python函数和变量名解析

1、Python函数 函数是Python为了代码最大程度的重用和最小化代码冗余而提供的基本程序结构,用于将相关功能打包并参数化 Python中可以创建4种函数: 1)、全局函数:定义在...