Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jehiah
Created September 24, 2010 15:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jehiah/595560 to your computer and use it in GitHub Desktop.
Save jehiah/595560 to your computer and use it in GitHub Desktop.
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