SHARE
TWEET

Untitled

a guest Apr 26th, 2018 70 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import json, time
  2. import mysql.connector as mysql
  3.  
  4. class Database:
  5.     def __init__(self, user=None, password=None, host=None, port=3306, schema=None, debug=False):
  6.         self.user = user
  7.         self.password = password
  8.         self.host = host
  9.         self.port = port
  10.         self.schema = schema
  11.         self.debug = debug
  12.    
  13.     def new_connection(self):
  14.         if self.debug:
  15.             print "[ CONNECTING TO MYSQL %s@%s:%d/%s ]" % (self.user, self.host, int(self.port), self.schema)
  16.         return mysql.connect(user=self.user, password=self.password, host=self.host, port=self.port, database=self.schema)
  17.    
  18.     def execute(self, sql):
  19.         res = False
  20.         try:
  21.             cnx = self.new_connection()
  22.             cur = cnx.cursor()
  23.  
  24.             if self.debug:
  25.                 print "[ EXECUTING QUERY: %s ]" % (sql,)
  26.  
  27.             cur.execute(sql)
  28.  
  29.             if not cnx._get_warnings:
  30.                 cnx.commit()
  31.                
  32.             cur.close()
  33.  
  34.             res = True
  35.         finally:
  36.             if cnx.is_connected():
  37.                 cnx.close()
  38.        
  39.         return res
  40.    
  41.     def query(self, sql):
  42.         res = None
  43.  
  44.         try:
  45.             cnx = self.new_connection()
  46.             cur = cnx.cursor()
  47.             cur.execute(sql)
  48.             res = cur.fetchall()
  49.             cur.close()
  50.         finally:
  51.             if cnx.is_connected():
  52.                 cnx.close()
  53.        
  54.         return res
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top