#!/usr/bin/env python2.7
#-*- coding:utf8 -*- import os
import sys
import logging
import MySQLdb from log import Logger logger=Logger("../log/",logging.DEBUG,"mysql_base.log") class MySQLBase(object):
"""MySQLBase""" def __init__(self,host,port,user,passwd,db='',charset='utf8',connect_timeout=1,retry_time=3,use_unicode=1):
self.__host = host
self.__port = port
self.__user = user
self.__passwd = passwd
self.__db = db
self.__charset = charset
self.__connect_timeout = connect_timeout
self.__retry_time = retry_time
self.__use_unicode = use_unicode
self.__connection = self._connection() def _connection(self):
connection = None for i in xrange(0,self.__retry_time):
try:
connection = MySQLdb.connect(host=self.__host,port=int(self.__port),user=self.__user,passwd=self.__passwd,db=self.__db,connect_timeout=self.__connect_timeout,charset=self.__charset,use_unicode=self.__use_unicode)
break
except Exception,e:
if i == self.__retry_time - 1:
message = "Failed to connect to host=%s,port=%s,Error_No:%d,\"%s\"" % (self.__host,self.__port,e[0],str(e[1])) logger.error(message)
print message
#os._exit(1)
            exit(1) continue
return connection def cursor(self):
connection = self.__connection cursor = connection.cursor(cursorclass=MySQLdb.cursors.DictCursor)
cursor.execute("set interactive_timeout=28800;")
cursor.execute("set wait_timeout=288000;") return cursor
def close(self):
connection = self.__connection if connection:
connection.close() def main():
host = "127.0.0.1"
port = ""
user = "pt"
passwd="" mysql_base = MySQLBase(host=host,port=port,user=user,passwd=passwd) cursor = mysql_base.cursor() sql = "show databases" cursor.execute(sql) result = cursor.fetchall() for row in result:
print row mysql_base.close()
if __name__ == "__main__":
main()

上述代码对于mysqldb简单封装了一下,更多使用介绍参照

http://zetcode.com/db/mysqlpython/

http://mysql-python.sourceforge.net/MySQLdb.html

Python mysqldb模块的更多相关文章

  1. Python MySQLdb模块连接操作mysql数据库实例_python

    mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法.python操作数据库需要安装一个第三方的模块,在http://mysql ...

  2. Python MySQLdb 模块

    MySQLdb 是 Python2 连接 MySQL 的一个模块,常见用法如下: [root@localhost ~]$ yum install -y MySQL-python # 安装 MySQLd ...

  3. python mysqldb 模块学习

    一.安装(环境win7 .python2.7) Python2.x 版本,使用MySQL-python: 安装包:MySQL-python-1.2.5.win32-py2.7.exe(双击安装) 下载 ...

  4. Python MySQLdb 模块使用方法

    import MySQLdb 2.和数据库建立连接 conn=MySQLdb.connect(host="localhost",user="root",pass ...

  5. Windows 安装 python MySQLdb模块

    pip install wheel 去这个网站查找whl格式的MYSQL-python http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python ...

  6. ubuntu安装python MySQLdb模块

    本文讲述了python安装mysql-python的方法.分享给大家供大家参考,具体如下: ubuntu 系统下进行的操作 首先安装了pip工具 ? 1 sudo apt-get install py ...

  7. python MySQLdb用法,python中cursor操作数据库(转)

    数据库连接 连接数据库前,请先确认以下事项: 您已经创建了数据库 TESTDB. 在TESTDB数据库中您已经创建了表 EMPLOYEE EMPLOYEE表字段为 FIRST_NAME, LAST_N ...

  8. Python的MySQLdb模块安装

    MySQL-python-1.2.1.tar.gz  下载地址:https://pan.baidu.com/s/1kVfH84v 然后解压,打开README(这个其实没有什么鸟用) 里面有安装过程: ...

  9. python安装MySQLdb模块

    以Ubuntu下安装为例: 下载地址:https://pypi.python.org/pypi/MySQL-python/ 解压后直接进入解压目录运行安装命令. python setup.py ins ...

随机推荐

  1. WPF 快捷方式

    http://www.cnblogs.com/gnielee/archive/2010/07/16/wpf-custom-hotkey-command.html

  2. 通过VBA,当在EXCEL单元格中输入任意的日期格式时,都能自动转换为指定的标准格式的日期值

    在日常录入EXCEL表格的单元格里 ,我们输入一些一般性的日期内容,如:2017-10-17 或 2017/10/17时,EXCEL会自动识别为日期并按单元格设计格式显示,单元格中存储的值也是日期格式 ...

  3. win10 uwp 绑定密码

    win10 下,密码框无法绑定到ViewModel,Password是不可以绑定. 我们可以自己使用简单方法去绑定 我们之前在WPF 使用绑定密码框,我写了一篇,关于如何绑定,我提供一个我自己试了可以 ...

  4. 【读书笔记】《写给大忙人看的Java SE 8》——Java8新特性总结

    虽然看过一些Java 8新特性的资料,但是平时很少用到,时间长了就忘了,正好借着Java 9的发布,来总结下一些Java 8中的新特性. 接口中的默认方法和静态方法 先考虑一个问题,如何向Java中的 ...

  5. ELK简介

    什么是ELK ELK是ElasticSearch,LogStash以及Kibana三个产品的首字母缩写.是可以和商业产品 Splunk 相媲美开源项目. 2013 年,Logstash 被 Elast ...

  6. APP崩溃提示:This application is modifying the autolayout engine from a background thread after the engine was accessed from the main thread. This can lead to engine corruption and weird crashes.

    崩溃输出日志 2017-08-29 14:53:47.332368+0800 HuiDaiKe[2373:1135604] This application is modifying the auto ...

  7. 【转】C语言中动态分配数组

    原文地址:http://blog.chinaunix.net/uid-11085590-id-2914577.html 如何动态的定义及使用数组呢?记得一般用数组的时候都是先指定大小的.当时问老师,老 ...

  8. [译]ASP.NET Core 2.0 全局配置项

    问题 如何在 ASP.NET Core 2.0 应用程序中读取全局配置项? 答案 首先新建一个空项目,并添加两个配置文件: 1. appsettings.json { "Section1&q ...

  9. Mongodb的mongostat命令

    Mongodb的mongostat命令可实时(1秒钟刷新一次)显示Mongodb数据库的运行情况,可视为性能监视器. 1.启动命令:authenticationDatabase表示用户认证证书所在的数 ...

  10. LeetCode 485. Max Consecutive Ones (最长连续1)

    Given a binary array, find the maximum number of consecutive 1s in this array. Example 1: Input: [1, ...