Remove database connection and schema setup from the project
This commit is contained in:
@@ -1,34 +0,0 @@
|
||||
# To connect to PostgreSQL database
|
||||
import psycopg2
|
||||
|
||||
from psycopg2.extras import RealDictCursor
|
||||
from typing import Optional
|
||||
|
||||
class Database:
|
||||
def __init__(self, db_name: str, user: str, password: str, host: str = 'localhost', port: int = 5432):
|
||||
self.connection = psycopg2.connect(
|
||||
dbname=db_name,
|
||||
user=user,
|
||||
password=password,
|
||||
host=host,
|
||||
port=port
|
||||
)
|
||||
self.connection.autocommit = True
|
||||
|
||||
def execute_query(self, query: str, params: Optional[tuple] = None):
|
||||
with self.connection.cursor(cursor_factory=RealDictCursor) as cursor:
|
||||
cursor.execute(query, params)
|
||||
if cursor.description:
|
||||
return cursor.fetchall()
|
||||
return []
|
||||
|
||||
def execute_many(self, query: str, params_list: list[tuple]):
|
||||
with self.connection.cursor(cursor_factory=RealDictCursor) as cursor:
|
||||
cursor.executemany(query, params_list)
|
||||
|
||||
def close(self):
|
||||
self.connection.close()
|
||||
print("Database connection closed.")
|
||||
|
||||
def __exit__(self, exc_type, exc_val, exc_tb):
|
||||
self.close()
|
||||
@@ -1,16 +0,0 @@
|
||||
CREATE SCHEMA IF NOT EXISTS ethnograph;
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ethnograph.users (
|
||||
id SERIAL PRIMARY KEY,
|
||||
username VARCHAR(255) UNIQUE NOT NULL,
|
||||
created_utc TIMESTAMP NOT NULL,
|
||||
karma INTEGER
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS ethnograph.posts (
|
||||
id SERIAL PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
content TEXT NOT NULL,
|
||||
author_username VARCHAR(255),
|
||||
created_utc TIMESTAMP NOT NULL
|
||||
);
|
||||
Reference in New Issue
Block a user