write read functionality
This commit is contained in:
parent
83f1dc6a9f
commit
72831a8e1d
17
database.py
17
database.py
@ -3,13 +3,14 @@ conditions (i.e. missing columns) without terminating the entire program. Use th
|
|||||||
handle database interactions, either as a standalone object or in a context manager."""
|
handle database interactions, either as a standalone object or in a context manager."""
|
||||||
import os
|
import os
|
||||||
import psycopg2
|
import psycopg2
|
||||||
|
from psycopg2 import DatabaseError
|
||||||
|
|
||||||
DB_ADDRESS = os.getenv('DB_ADDRESS', 'localhost')
|
DB_ADDRESS = os.getenv('DB_ADDRESS', 'localhost')
|
||||||
DB_PORT = os.getenv('DB_PORT', 5432)
|
DB_PORT = os.getenv('DB_PORT', 5432)
|
||||||
DB_USER = os.getenv('DB_USER', 'postgres')
|
DB_USER = os.getenv('DB_USER', 'postgres')
|
||||||
DB_PASSWORD = os.getenv('DB_PASSWORD', '')
|
DB_PASSWORD = os.getenv('DB_PASSWORD', '')
|
||||||
DB_NAME = os.getenv('DB_NAME', 'postgres')
|
DB_NAME = os.getenv('DB_NAME', 'postgres')
|
||||||
|
DB_TABLE = os.getenv('DB_TABLE', 'cables')
|
||||||
|
|
||||||
|
|
||||||
class DBConnector:
|
class DBConnector:
|
||||||
@ -21,9 +22,12 @@ class DBConnector:
|
|||||||
db.read()
|
db.read()
|
||||||
"""
|
"""
|
||||||
def _db_start(self):
|
def _db_start(self):
|
||||||
|
try:
|
||||||
self.conn = psycopg2.connect(
|
self.conn = psycopg2.connect(
|
||||||
f"host={DB_ADDRESS} port={DB_PORT} dbname={DB_NAME} user={DB_USER} password={DB_PASSWORD}")
|
f"host={DB_ADDRESS} port={DB_PORT} dbname={DB_NAME} user={DB_USER} password={DB_PASSWORD}")
|
||||||
self.cur = self.conn.cursor()
|
self.cur = self.conn.cursor()
|
||||||
|
except DatabaseError as e:
|
||||||
|
|
||||||
|
|
||||||
def _db_stop(self):
|
def _db_stop(self):
|
||||||
self.cur.close()
|
self.cur.close()
|
||||||
@ -45,7 +49,8 @@ class DBConnector:
|
|||||||
try:
|
try:
|
||||||
self.cur.execute(sql)
|
self.cur.execute(sql)
|
||||||
result = self.cur.fetchall()
|
result = self.cur.fetchall()
|
||||||
except psycopg2.DatabaseError as e:
|
except DatabaseError as e:
|
||||||
|
print(f"DB ERROR [{e.pgcode}]: {e.pgerror}")
|
||||||
result = []
|
result = []
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@ -54,7 +59,13 @@ class DBConnector:
|
|||||||
|
|
||||||
:param kwargs: Column constraints; i.e. what value to filter by in what column.
|
:param kwargs: Column constraints; i.e. what value to filter by in what column.
|
||||||
:returns: A list of dictionaries of all matching rows, or an empty list if no match."""
|
:returns: A list of dictionaries of all matching rows, or an empty list if no match."""
|
||||||
pass
|
args = []
|
||||||
|
for kw in kwargs.keys():
|
||||||
|
args.append(f"{kw} ILIKE {kwargs['kw']}")
|
||||||
|
query = f"SELECT * FROM {DB_TABLE}"
|
||||||
|
if len(args) > 0:
|
||||||
|
query += f" WHERE {' AND '.join(args)}"
|
||||||
|
return self._query(query)
|
||||||
|
|
||||||
def write(self, **kwargs):
|
def write(self, **kwargs):
|
||||||
"""Write a row to the database.
|
"""Write a row to the database.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user