79 lines
2.2 KiB
Python
79 lines
2.2 KiB
Python
import sqlite3
|
|
|
|
|
|
class DatabaseMain():
|
|
|
|
def __init__(self):
|
|
self.database_name = 'towary_main.db'
|
|
self.conn = None
|
|
self.c = None
|
|
|
|
def _create_or_connect_to_db(self):
|
|
# create a database or connect to one
|
|
self.conn = sqlite3.connect(self.database_name)
|
|
|
|
def _create_cursor(self):
|
|
# create cursor
|
|
self.c = self.conn.cursor()
|
|
|
|
def _create_table(self):
|
|
self.c.execute("""CREATE TABLE IF NOT EXISTS computers (
|
|
com_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
com_brand text,
|
|
com_model text,
|
|
com_cpu text,
|
|
com_ram text
|
|
)""")
|
|
|
|
def _show_table(self):
|
|
self.c.execute("SELECT * FROM computers")
|
|
print(self.c.fetchall())
|
|
|
|
def _insert_value_to_table(self, parsed_com_brand, parsed_com_model, parsed_com_cpu, parsed_com_ram):
|
|
self.c.execute(f"""INSERT INTO COMPUTERS (
|
|
com_brand,
|
|
com_model,
|
|
com_cpu,
|
|
com_ram
|
|
)
|
|
VALUES (
|
|
"{parsed_com_brand}",
|
|
"{parsed_com_model}",
|
|
"{parsed_com_cpu}",
|
|
"{parsed_com_ram}"
|
|
)""")
|
|
|
|
def _commit_to_db(self):
|
|
self.conn.commit()
|
|
|
|
def _close_db(self):
|
|
self.conn.close()
|
|
|
|
def initialize_db(self):
|
|
self._create_or_connect_to_db()
|
|
self._create_cursor()
|
|
self._create_table()
|
|
self._commit_to_db()
|
|
self._close_db()
|
|
|
|
def show_db(self):
|
|
self._create_or_connect_to_db()
|
|
self._create_cursor()
|
|
self._show_table()
|
|
self._close_db()
|
|
|
|
def insert_data(self, parsed_com_brand, parsed_com_model, parsed_com_cpu, parsed_com_ram):
|
|
self._create_or_connect_to_db()
|
|
self._create_cursor()
|
|
self._insert_value_to_table(
|
|
parsed_com_brand, parsed_com_model, parsed_com_cpu, parsed_com_ram)
|
|
self._commit_to_db()
|
|
self._close_db()
|
|
|
|
def drop_db(self):
|
|
self._create_or_connect_to_db()
|
|
self._create_cursor()
|
|
self.c.execute("DROP TABLE computers")
|
|
self._commit_to_db()
|
|
self._close_db()
|