Python mysqldb模块
#!/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模块的更多相关文章
- Python MySQLdb模块连接操作mysql数据库实例_python
mysql是一个优秀的开源数据库,它现在的应用非常的广泛,因此很有必要简单的介绍一下用python操作mysql数据库的方法.python操作数据库需要安装一个第三方的模块,在http://mysql ...
- Python MySQLdb 模块
MySQLdb 是 Python2 连接 MySQL 的一个模块,常见用法如下: [root@localhost ~]$ yum install -y MySQL-python # 安装 MySQLd ...
- python mysqldb 模块学习
一.安装(环境win7 .python2.7) Python2.x 版本,使用MySQL-python: 安装包:MySQL-python-1.2.5.win32-py2.7.exe(双击安装) 下载 ...
- Python MySQLdb 模块使用方法
import MySQLdb 2.和数据库建立连接 conn=MySQLdb.connect(host="localhost",user="root",pass ...
- Windows 安装 python MySQLdb模块
pip install wheel 去这个网站查找whl格式的MYSQL-python http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python ...
- ubuntu安装python MySQLdb模块
本文讲述了python安装mysql-python的方法.分享给大家供大家参考,具体如下: ubuntu 系统下进行的操作 首先安装了pip工具 ? 1 sudo apt-get install py ...
- python MySQLdb用法,python中cursor操作数据库(转)
数据库连接 连接数据库前,请先确认以下事项: 您已经创建了数据库 TESTDB. 在TESTDB数据库中您已经创建了表 EMPLOYEE EMPLOYEE表字段为 FIRST_NAME, LAST_N ...
- Python的MySQLdb模块安装
MySQL-python-1.2.1.tar.gz 下载地址:https://pan.baidu.com/s/1kVfH84v 然后解压,打开README(这个其实没有什么鸟用) 里面有安装过程: ...
- python安装MySQLdb模块
以Ubuntu下安装为例: 下载地址:https://pypi.python.org/pypi/MySQL-python/ 解压后直接进入解压目录运行安装命令. python setup.py ins ...
随机推荐
- zoj1797 Least Common Multiple 最小公倍数
Least Common Multiple Time Limit: 2 Seconds Memory Limit: 65536 KB The least common multiple (L ...
- Java面向对象(封装性概论)
Java面向对象(封装性概论) 知识概要: (1)面向对象概念 (2)类与对象的关系 (3)封装 (4)构造函数 (5)this关键字 (6)static关键 ...
- DataRow和DataRowView的区别
可以将DataView同数据库的视图类比,不过有点不同,数据库的视图可以跨表建立视图,DataView则只能对某一个DataTable建立视图. DataView一般通过DataTable.Defau ...
- pb9常见错误及含义
1. by zero 发生被0除错误 2. Null object reference 空对象引用 3. Array boundary exceeded 数组越界 4. Enumerated v ...
- 即时通信系统Openfire分析之七:集群配置
前言 写这章之前,我犹豫了一会.在这个时候提集群,从章节安排上来讲,是否合适?但想到上一章<路由表>的相关内容,应该不至于太突兀.既然这样,那就撸起袖子干吧. Openfire的单机并发量 ...
- Akka(28): Http:About Akka-Http
众所周知,Akka系统是基于Actor模式的分布式运算系统,非常适合构建大数据平台.所以,无可避免地会出现独立系统之间.与异类系统.与移动系统集成的需求.由于涉及到异类和移动系统,系统对接的方式必须在 ...
- [Python] Codecombat 攻略 Sarven 沙漠 (1-43关)截止至30关
首页:https://cn.codecombat.com/play语言:Python 第二界面:Sarven沙漠(43关)时间:4-11小时内容:算术运算,计数器,while循环,break(跳出循环 ...
- 浅谈前后端分离与实践 之 nodejs 中间层服务(二)
一.背景 书接上文,浅谈前后端分离与实践(一) 我们用mock服务器搭建起来了自己的前端数据模拟服务,前后端开发过程中只需定义好接口规范,便可以相互进行各自的开发任务.联调的时候,按照之前定义的开发规 ...
- $(window).on("load",function(){} 和 $(document).ready(function() {}
$(window).on("load",function(){ //页面属性,图片,内容完全加载完,执行 } $(document).ready(function() { 或者$( ...
- 小技巧:Oracle:sqlplus 显示行列字符数
遇到这种情况可以判断:行显示字符数不够,可以增加行显示字符数 01.可以当前会话HR@ACE >set line 400; 02.上面的方法其它会话不生效,懒不想每次设置怎么办? Oracle: ...