/python3-sqlite

Library to manage a sqlite database

Primary LanguagePythonGNU General Public License v3.0GPL-3.0

python3-sqlite

This library provides an easy way to manage a sqlite database. To do this, use the sqlite3 library to connect to the database and the logging library to display information about errors and debugging.

Installation

Installation can be done through the pip3 command:

pip3 install procamora-sqlite3 --user

You can also update the library with:

python3 -m pip install --user --upgrade procamora-sqlite3

Basic Usage

To use this class the first thing to do is import the library:

from procamora_sqlite3.logger import get_logger, logging
from procamora_sqlite3.interface_sqlite import *

The interface_sqlite file when doing an import * we are importing three functions, these are:

__all__ = ['conection_sqlite', 'execute_script_sqlite', 'dump_database']

conection_sqlite

This function is responsible for carrying out the main SQL operations, such as: SELECT, INSERT, UPDATE or DELETE.

An example of some of these functions would be:

def select_all_hosts() -> List[Dict[Text, Any]]:
    query: Text = "SELECT * FROM Hosts"
    response_query: List[Dict[Text, Any]] = conection_sqlite(self.db, query, is_dict=True)
    return response_query

def update_host_offline(date: Text):
    query: Text = f"UPDATE Hosts SET active=0 WHERE date <> '{date}';"
    conection_sqlite(self.db, query)

execute_script_sqlite

This function allows you to run a script or dump that you receive in string format. With this function, databases could be created.

execute_script_sqlite(self.db, self.dump.read_text())

dump_database

This function allows you to perform a complete dump of the database.

response = dump_database(self.db)