# sudo chmod 664 /var/www/xala.dev/db_xala_dev/xala_dev.db
# sudo chown -R www-data:www-data /var/www/xala.dev/db_xala_dev
# sudo chmod -R 775 /var/www/xala.dev/db_xala_dev
# sudo chmod 755 /var/www/xala.dev/db_xala_dev
# sudo chmod 750 /var/www/xala.dev/db_xala_dev


# sudo chown www-data:www-data /var/www/xala.dev/db_xala_dev/xala_dev.db
# sudo chmod 660 /var/www/xala.dev/db_xala_dev/xala_dev.db
# sudo chown www-data:www-data /var/www/xala.dev/db_xala_dev
# sudo chmod 770 /var/www/xala.dev/db_xala_dev

import sqlite3

class DBSQLite:
    def __init__(self, db_path):
        self.db_path = db_path

    def _connect(self):
        """Crea y retorna una conexión a la base de datos."""
        try:
            return sqlite3.connect(self.db_path)
        except sqlite3.Error as e:
            print(f"Error al conectar con la base de datos: {e}")
            return None

    def get_data(self, query):
        """Ejecuta una consulta SELECT y retorna los resultados."""
        conn = self._connect()
        if not conn:
            return None
        try:
            with conn:
                cursor = conn.cursor()
                cursor.execute(query)
                return cursor.fetchall()
        except sqlite3.Error as e:
            print(f"Error en la consulta: {e}")
            return None
        finally:
            conn.close()

    def execute_query(self, query, params=()):
        """Ejecuta una consulta INSERT, UPDATE o DELETE."""
        conn = self._connect()
        if not conn:
            return False
        try:
            with conn:
                cursor = conn.cursor()
                cursor.execute(query, params)
            return True
        except sqlite3.Error as e:
            print(f"Error al ejecutar la consulta: {e}")
            return False
        finally:
            conn.close()
