Initial commit
This commit is contained in:
commit
ff50ea6784
12 changed files with 509 additions and 0 deletions
36
models/database.py
Normal file
36
models/database.py
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
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 fetch_all(self, query: str, params: tuple = ()):
|
||||
with self.connection.cursor() as cursor:
|
||||
cursor.execute(query, params)
|
||||
return cursor.fetchall()
|
||||
|
||||
def fetch_one(self, query: str, params: tuple = ()):
|
||||
with self.connection.cursor() as cursor:
|
||||
cursor.execute(query, params)
|
||||
return cursor.fetchone()
|
||||
|
||||
def execute(self, query: str, params: tuple = ()):
|
||||
with self.connection.cursor() as cursor:
|
||||
cursor.execute(query, params)
|
||||
self.connection.commit()
|
||||
Loading…
Add table
Add a link
Reference in a new issue