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 ...
随机推荐
- ZOJ2212 Argus 优先队列 结构体
#include <iostream> #include <string> #include <queue> using namespace std; struct ...
- Java 继承、抽象、接口
一.继承 1. 概述 继承是面向对象的重要特征之一,当多个类中存在相同的属性和行为时,将这些内容抽取到单独一个类中,那多个类中无需再定义这些属性和行为,只需继承那个单独的类即可. 单独的类称为父类或超 ...
- wpf 中英文版编写
var uriC = new Uri("/YTManage.Language;component/Chinese.xaml", UriKind.Relative); // 得到资源 ...
- redis性能调优笔记(can not get Resource from jedis pool和jedis connect time out)
对这段时间redis性能调优做一个记录. 1.单进程单线程 redis是单进程单线程实现的,如果你没有特殊的配置,redis内部默认是FIFO排队,即你对redis的访问都是要在redis进行排队,先 ...
- IOS应用FFMPEG库
1.引用资源 build-ffmpeg ffmpeg库生成 -sh开源地址: https://gist.github.com/m1entus/6983547 iFrameExtractor ffmp ...
- JIRA-6.3.6安装与破解
首先下载JIRA-6.3.6的安装包: wget http://www.atlassian.com/software/jira/downloads/binary/atlassian-jira-6.3. ...
- 吾八哥学Python(三):了解Python基础语法(上)
学习一门开发语言首先当然是要熟悉它的语法了,Python的语法还算是比较简单的,这里从基础的开始了解一下. 标识符1.第一个字符必须是字母表中字母或下划线'_'.2.标识符的其他的部分有字母.数字和下 ...
- java笔记02
一,编写一个方法,使用以上算法生成指定数目(比如1000个)的随机整数 /** * */ package 课堂2; import java.util.Random; /** * @author 信16 ...
- LeetCode 217. Contains Duplicate (包含重复项)
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- Java企业微信开发_Exception_02_java.security.InvalidKeyException: Illegal key size
今天换了重新装了一个jdk,然后运行昨天还好好的企业微信工程,结果启动的时候就给我报了这么个错: java.security.InvalidKeyException: Illegal key size ...