Python操作Mysql数据库的实现

简介:
+关注继续查看

1、需要使用的模块MySQLdb,下载地址为:http://sourceforge.net/projects/mysql-python/

2、实现代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#!/usr/bin/env python
# encoding: utf-8
 
import MySQLdb
 
# 数据操作类
class DBHelper:
    """
    数据操作类,提供数据操作的方法
    """
    def __init__(self, host, user, password, port, database):
        self.host = host
        self.user = user
        self.port = port
        self.password = password
        self.database = database
 
    def call_procedure(self, procedure_name, tuple_parameters):
        """
        procedure_name: 被调用的存储过程
        tuple_parameters: 使用的参数
        返回结果:
        """
        try:
            conn = MySQLdb.connect(host=self.host,
                                   user=self.user,
                                   passwd=self.password,
                                   port=self.port,
                                   db=self.database)
            conn.autocommit(True)
            cur = conn.cursor()
            cur.callproc(procedure_name, tuple_parameters)
            data = cur.fetchall()
            cur.close()
            conn.close()
            return data
        except MySQLdb.Error, e:
            print "存储过程执行出错: %d: %s" % (e.args[0], e.args[1])
            return None

    



本文转自 许大树 51CTO博客,原文链接:http://blog.51cto.com/abelxu/1872988,如需转载请自行联系原作者

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
相关文章
|
8月前
|
JSON 区块链 数据格式
Python实现一个简单的区块链
本文介绍如何用Python实现一个简单的区块链。
351 0
|
8月前
|
存储 数据安全/隐私保护 计算机视觉
python 实现pacs功能 推送下拉影像
python 实现dcmtk关联pacs功能 推送下拉影像
170 0
python 实现pacs功能 推送下拉影像
|
8月前
|
算法 大数据 Python
Leedcode 每日一练 搜索二维矩阵Ⅰ Python实现
Leedcode 每日一练 搜索二维矩阵Ⅰ Python实现
81 2
Leedcode 每日一练 搜索二维矩阵Ⅰ Python实现
|
8月前
|
前端开发 Python
Leecode加法题目3个 每日练习 Python实现
Leecode加法题目3个 每日练习 Python实现
59 0
Leecode加法题目3个 每日练习 Python实现
|
8月前
|
iOS开发 Python
Python实现微信消息连续发送
Python实现微信消息连续发送
Python实现微信消息连续发送
|
8月前
|
Python
Python print() 打印两个 list ,实现中间换行
Python print() 打印两个 list ,实现中间换行
|
8月前
|
Python
python实现微信小游戏“飞机大战”
python实现微信小游戏“飞机大战”
python实现微信小游戏“飞机大战”
|
8月前
|
Python
Python分分钟实现图书管理系统(含代码)
Python分分钟实现图书管理系统(含代码)
178 0
|
8月前
|
JSON 算法 数据安全/隐私保护
Python:使用PyJWT实现JSON Web Tokens加密解密
Python:使用PyJWT实现JSON Web Tokens加密解密
169 0
|
9月前
|
Python
Python实现因子分析(附案例实战)
Python实现因子分析(附案例实战)
529 0
Python实现因子分析(附案例实战)
推荐文章
更多
推荐镜像
更多