23 lines
665 B
Python
23 lines
665 B
Python
import psycopg2
|
|
|
|
|
|
class Database:
|
|
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 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, sql, params=None):
|
|
with self.conn.cursor() as cur:
|
|
cur.execute(sql, params or ())
|
|
return cur.fetchone()
|
|
|
|
def commit(self):
|
|
self.conn.commit()
|