filter's fix
This commit is contained in:
parent
ff50ea6784
commit
4b4acdcb1b
6 changed files with 144 additions and 171 deletions
|
|
@ -1,36 +1,23 @@
|
|||
import psycopg2
|
||||
from psycopg2.extras import RealDictCursor
|
||||
from config import (
|
||||
DB_HOST,
|
||||
DB_PORT,
|
||||
DB_NAME,
|
||||
DB_USER,
|
||||
DB_PASSWORD,
|
||||
)
|
||||
|
||||
|
||||
class Database:
|
||||
def __init__(self):
|
||||
self.connection = psycopg2.connect(
|
||||
host=DB_HOST,
|
||||
port=DB_PORT,
|
||||
dbname=DB_NAME,
|
||||
user=DB_USER,
|
||||
password=DB_PASSWORD,
|
||||
cursor_factory=RealDictCursor,
|
||||
def __init__(self, dbname, user, password, host="localhost", port=5432):
|
||||
self.conn = psycopg2.connect(
|
||||
dbname=dbname, user=user, password=password, host=host, port=port
|
||||
)
|
||||
|
||||
def fetch_all(self, query: str, params: tuple = ()):
|
||||
with self.connection.cursor() as cursor:
|
||||
cursor.execute(query, params)
|
||||
return cursor.fetchall()
|
||||
def query(self, sql, params=None):
|
||||
with self.conn.cursor() as cur:
|
||||
cur.execute(sql, params or ())
|
||||
if cur.description:
|
||||
return cur.fetchall()
|
||||
return None
|
||||
|
||||
def fetch_one(self, query: str, params: tuple = ()):
|
||||
with self.connection.cursor() as cursor:
|
||||
cursor.execute(query, params)
|
||||
return cursor.fetchone()
|
||||
def fetch_one(self, sql, params=None):
|
||||
with self.conn.cursor() as cur:
|
||||
cur.execute(sql, params or ())
|
||||
return cur.fetchone()
|
||||
|
||||
def execute(self, query: str, params: tuple = ()):
|
||||
with self.connection.cursor() as cursor:
|
||||
cursor.execute(query, params)
|
||||
self.connection.commit()
|
||||
def commit(self):
|
||||
self.conn.commit()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue