Advertisement
GastonPalazzo

dba.py

Nov 25th, 2020 (edited)
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.16 KB | None | 0 0
  1. # import
  2. import mysql.connector
  3.  
  4. # db config
  5. dbconf={
  6.     'host':'localhost',
  7.     'user':'root',
  8.     'password':'',
  9.     'database':'redsocialbd'
  10. }
  11.  
  12. # class
  13. class DataBase():
  14.     # cnstr
  15.     def __init__(self):
  16.         self.__mydb = mysql.connector.connect(**dbconf)
  17.         self.__cursor = self.__mydb.cursor()
  18.     # mthds
  19.     # gtt
  20.     def get_lastrowid(self):
  21.         return self.__cursor.lastrowid
  22.     def get_mydb(self):
  23.         return self.__mydb
  24.     def get_cursor(self):
  25.         return self.__cursor
  26.     def get_commit(self):
  27.         return self.__mydb.commit()
  28.     # especificos
  29.     def ejecutar(self, qry, val):
  30.         self.__cursor.execute(qry, val)
  31.     def actualizar_auto_increment(self, tab=str, col=str):
  32.         qry = 'SELECT MAX('+col+')+%s FROM '+tab+';'
  33.         val = (1, )
  34.         self.__cursor.execute(qry, val)
  35.         r = self.__cursor.fetchone()[0]
  36.         if r != None:
  37.             qry = 'ALTER TABLE '+tab+' AUTO_INCREMENT = %s;'
  38.             val = (r, )
  39.         else:
  40.             qry = 'ALTER TABLE '+tab+' AUTO_INCREMENT = %s;'
  41.             val = (1, )
  42.         self.__cursor.execute(qry, val)
  43.         self.get_commit()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement