neildd
2012-11-22 10:17:45 +08:00
import MySQLdb
class MyDBClass:
def __init__(self, db_host, db_user, db_pass, db_name, db_charset='UTF8'):
self.db_host = db_host
self.db_user = db_user
self.db_pass = db_pass
self.db_name = db_name
self.db_charset = db_charset
self.db = None
def db_connect(self):
self.db = MySQLdb.connect(host=self.db_host, user=self.db_user, passwd=self.db_pass, db=self.db_name, charset=self.db_charset)
self.cursor = self.db.cursor()
def db_ping(self):
if not self.db:
self.db_connect()
else:
try:
self.db.ping()
except:
self.db_connect()
def db_fetchone(self, *args):
self.db_ping()
self.cursor.execute(*args)
ret = self.cursor.fetchone()
return ret
def db_fetchall(self, *args):
self.db_ping()
self.cursor.execute(*args)
ret = self.cursor.fetchall()
return ret
def db_execute(self, *args):
self.db_ping()
self.cursor.execute(*args)
return self.db.insert_id()