Advertisement
Guest User

Untitled

a guest
May 28th, 2017
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. class DB():
  2.    
  3.     db = None
  4.    
  5.     def __init__(self, mysqlhost, mysqluser, mysqlpass, mysqldb):
  6.         self.mysqlhost = mysqlhost
  7.         self.mysqluser = mysqluser
  8.         self.mysqlpass = mysqlpass
  9.         self.mysqldb = mysqldb
  10.    
  11.     def connect(self):
  12.         self.db = MySQLdb.connect(host=mysqlhost, user=mysqluser, passwd=mysqlpass, db=mysqldb)
  13.        
  14.     def query(self, sql):
  15.         try:
  16.             cursor = self.db.cursor()
  17.             cursor.execute(sql)
  18.         except(AttributeError, MySQLdb.OperationalError):
  19.             self.connect()
  20.             cursor = self.db.cursor()
  21.             cursor.execute(sql)
  22.         return cursor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement