mirror of
https://github.com/th3r00t/pyShelf.git
synced 2026-04-28 01:59:35 -04:00
Switch to postgresql, and adapted existing queries
This commit is contained in:
2
pyproject.toml
vendored
2
pyproject.toml
vendored
@@ -7,4 +7,4 @@ use_parentheses = true
|
|||||||
# NOTE: the known_third_party setting is managed by
|
# NOTE: the known_third_party setting is managed by
|
||||||
# seed-isort-config and should not be modified directly.
|
# seed-isort-config and should not be modified directly.
|
||||||
# Any changes made to this setting will be overwritten.
|
# Any changes made to this setting will be overwritten.
|
||||||
known_third_party = ["bs4", "django", "interface", "requests"]
|
known_third_party = ["bs4", "django", "interface", "psycopg2", "requests"]
|
||||||
|
|||||||
@@ -1,8 +1,8 @@
|
|||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
import sqlite3
|
import sqlite3
|
||||||
import sys
|
|
||||||
|
|
||||||
# sys.path.insert(1, '../')
|
import psycopg2
|
||||||
|
|
||||||
from .config import Config
|
from .config import Config
|
||||||
|
|
||||||
# db_pointer = Config().catalogue_db
|
# db_pointer = Config().catalogue_db
|
||||||
@@ -11,22 +11,23 @@ from .config import Config
|
|||||||
class Storage:
|
class Storage:
|
||||||
"""Contains all methods for system storage"""
|
"""Contains all methods for system storage"""
|
||||||
|
|
||||||
def __init__(self, db_pointer):
|
def __init__(self, db_pointer, config):
|
||||||
# Optionaly pass db_file to specify another db or for testing
|
# self.db_file = db_pointer
|
||||||
if db_pointer is None:
|
self.sql = config.catalogue_db
|
||||||
db_pointer = Config().catalogue_db
|
self.user = config.user
|
||||||
self.db_file = db_pointer
|
self.password = config.password
|
||||||
self.database()
|
self.database()
|
||||||
# self.create_tables()
|
# self.create_tables()
|
||||||
|
|
||||||
def database(self):
|
def database(self):
|
||||||
"""Create database cursor"""
|
"""Create database cursor"""
|
||||||
try:
|
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()
|
self.cursor = self.db.cursor()
|
||||||
return True
|
return True
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(self.db_file)
|
print(self.sql)
|
||||||
print(e)
|
print(e)
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@@ -38,7 +39,7 @@ class Storage:
|
|||||||
file_name text)"""
|
file_name text)"""
|
||||||
try:
|
try:
|
||||||
self.cursor.execute(q_check)
|
self.cursor.execute(q_check)
|
||||||
except sqlite3.OperationalError as e:
|
except Exception as e:
|
||||||
self.cursor.execute(q_create)
|
self.cursor.execute(q_create)
|
||||||
|
|
||||||
def insert_book(self, book):
|
def insert_book(self, book):
|
||||||
@@ -46,8 +47,10 @@ class Storage:
|
|||||||
Insert book in database
|
Insert book in database
|
||||||
:returns: True if succeeds False if not
|
:returns: True if succeeds False if not
|
||||||
"""
|
"""
|
||||||
q_x = """SELECT title FROM books WHERE EXISTS(SELECT * from books WHERE `title` = ?)"""
|
q_x = (
|
||||||
q = """INSERT INTO books (title, author, cover, progress, file_name, pages) values (?, ?, ?, 0, ?, 0)"""
|
"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:
|
||||||
try:
|
try:
|
||||||
cover_image = book[2].data
|
cover_image = book[2].data
|
||||||
|
|||||||
Reference in New Issue
Block a user