diff --git a/pyproject.toml b/pyproject.toml index ffa6471..38d47ab 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -7,4 +7,4 @@ use_parentheses = true # NOTE: the known_third_party setting is managed by # seed-isort-config and should not be modified directly. # Any changes made to this setting will be overwritten. -known_third_party = ["bs4", "django", "interface", "requests"] +known_third_party = ["bs4", "django", "interface", "psycopg2", "requests"] diff --git a/src/backend/lib/storage.py b/src/backend/lib/storage.py index f652865..26b42a2 100755 --- a/src/backend/lib/storage.py +++ b/src/backend/lib/storage.py @@ -1,8 +1,8 @@ #!/usr/bin/python import sqlite3 -import sys -# sys.path.insert(1, '../') +import psycopg2 + from .config import Config # db_pointer = Config().catalogue_db @@ -11,22 +11,23 @@ from .config import Config class Storage: """Contains all methods for system storage""" - def __init__(self, db_pointer): - # Optionaly pass db_file to specify another db or for testing - if db_pointer is None: - db_pointer = Config().catalogue_db - self.db_file = db_pointer + def __init__(self, db_pointer, config): + # self.db_file = db_pointer + self.sql = config.catalogue_db + self.user = config.user + self.password = config.password self.database() # self.create_tables() def database(self): """Create database cursor""" try: - self.db = sqlite3.connect(self.db_file) + # self.db = sqlite3.connect(self.db_file) + self.db = psycopg2.connect(database=self.sql, user=self.user) self.cursor = self.db.cursor() return True except Exception as e: - print(self.db_file) + print(self.sql) print(e) return False @@ -38,7 +39,7 @@ class Storage: file_name text)""" try: self.cursor.execute(q_check) - except sqlite3.OperationalError as e: + except Exception as e: self.cursor.execute(q_create) def insert_book(self, book): @@ -46,8 +47,10 @@ class Storage: Insert book in database :returns: True if succeeds False if not """ - q_x = """SELECT title FROM books WHERE EXISTS(SELECT * from books WHERE `title` = ?)""" - q = """INSERT INTO books (title, author, cover, progress, file_name, pages) values (?, ?, ?, 0, ?, 0)""" + q_x = ( + "SELECT title FROM books WHERE EXISTS(SELECT * from books WHERE title = %s)" + ) + q = "INSERT INTO books (title, author, cover, progress, file_name, pages) values (%s, %s, %s, 0, %s, 0)" try: try: cover_image = book[2].data