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

yipeiwu_com5年前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解析并修改XML文档的方法

问题 你想读取一个XML文档,对它最一些修改,然后将结果写回XML文档。 解决方案 使用 xml.etree.ElementTree 模块可以很容易的处理这些任务。 第一步是以通常的方...

tensorflow实现打印ckpt模型保存下的变量名称及变量值

有时候会需要通过从保存下来的ckpt文件来观察其保存下来的训练完成的变量值。 ckpt文件名列表:(一般是三个文件) xxxxx.ckpt.data-00000-of-00001 xxx...

Tensorflow使用tfrecord输入数据格式

Tensorflow 提供了一种统一的格式来存储数据,这个格式就是TFRecord,上一篇文章中所提到的方法当数据的来源更复杂,每个样例中的信息更丰富的时候就很难有效的记录输入数据中的信...

python 随机打乱 图片和对应的标签方法

如下所示: # -*- coding: utf-8 -*- import os import numpy as np import pandas as pd import h5p...

深入浅析Python 中 is 语法带来的误解

起步 Python 的成功一个原因是它的可读性,代码清晰易懂,更容易被人类所理解,但有时可读性会产生误解。 假如要判断一个变量是不是 17,那可以: if x is 17: x 是 17...