psql.py 1.32 KB
import psycopg2

class BaseDatos():
    def __init__(self):
        self.HOST = "127.0.0.1" #"192.168.10.59"
        self.PORT = 5432
        self.USER = "administrador"
        self.PASS = "administrador"
        self.DB = "isvareporte"
        self.SELECT = ""
    

    def __conectarr__(self):
        self.conn = psycopg2.connect(host=self.HOST, port=self.PORT, 
        database=self.DB, user=self.USER, password=self.PASS)
        self.cur = self.conn.cursor()
        self.resultado = ""

    def __desconectar__(self):
        self.conn.close()
        self.cur.close()

    def __leer__(self):
        self.cur.execute(self.SELECT)
        self.resultado = self.cur.fetchall()

    def __ValidarComando__(self):
        i = True
        select = self.SELECT.split(" ")
        for palabra in select:
            if palabra == "DROP":
                i = False
            if palabra == "UPDATE":
                i = False
            if palabra == "INCERT":
                i = False
            if palabra == "drop":
                i = False
            if palabra == "update":
                i = False
            if palabra == "incert":
                i = False
        return(i)
    
    def Leer(self):
        if self.__ValidarComando__():
            self.__conectarr__()
            self.__leer__()
            self.__desconectar__()