Created
September 24, 2010 15:36
-
-
Save jehiah/595560 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import MySQLdb | |
from DBUtils import PooledDB | |
from MySQLdb import cursors | |
import settings | |
import logging | |
class DB(object): | |
"""simple database wrapper""" | |
mysqldbpool = None | |
@classmethod | |
def init_connection_settings(self): | |
cnf = settings.get('db') | |
DB.mysqldbpool = PooledDB.PooledDB(MySQLdb,mincached=1,maxcached=1, | |
connect_timeout=5, | |
cursorclass=cursors.DictCursor, | |
host=cnf['host'], | |
user=cnf['user'], | |
passwd=cnf['pass'], | |
db=cnf['name'], | |
charset='utf8') | |
@classmethod | |
def doSQL(self, *args, **kwargs): | |
return self.query(*args, **kwargs) | |
@classmethod | |
def query(self, sql, tuple_subs=None): | |
if DB.mysqldbpool is None: | |
DB.init_connection_settings() | |
db = DB.mysqldbpool.connection() | |
c = db.cursor() | |
try: | |
c.execute(sql, tuple_subs) | |
res = c.fetchall() | |
db.commit() | |
except: | |
db.rollback() | |
raise | |
return res |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment