加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

python 调用存储过程

(2010-02-05 12:02:21)
标签:

杂谈

分类: 技术
#执行SQL返回结果
class SqlResult( object ):
    ''
    '执行存储过程之后返回的结果。包括:受影响行数,返回的查询结果'
    ''
    def __init__( self ):
        self.hangshu = 0
        self.data = ()
        self.out = ()


class SQlAccess( object ):
    ''
    '调用存储过程。参数:存储过程名字,所需参数'
    ''
    def CallProcTest( self, procname, parameters ):
        #cur.callproc('SpilePage',('Admin','1=1',1,20))
        #是否有参数
        if len( parameters ) > 0:
            procname += '('
            for i in range( len( parameters ) ):
                procname += '%s,'
            procname = procname[0:len( procname ) - 1] + ')'

        #拼装字符串
        procname = 'call ' + procname

        result = SqlResult()
        #连接数据库
        conn = MySQLdb.connect( host = '92.11.5.1', user = 'mysql', passwd = '123456', db = 'zillioncms' )
        cur = conn.cursor()
        try:
            print procname
            #执行存储过程
            result.hangshu = cur.execute( procname , parameters )
            #cur.execute()
            result.data = cur.fetchall()
            cur.nextset()
            conn.commit()
        except Exception, e:
            print 'do somthing.'
        finally:
            cur.close()
            conn.close()
        return result

#调用
mys = SQlAccess()
muresult = mys.CallProcTest( 'SpilePage', ( 'Dictionary', '1=1', 1, 10 ) )

0

阅读 收藏 喜欢 打印举报/Report
前一篇:2009年06月03日
后一篇:python 验证码
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有