-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
70 lines (61 loc) · 2.04 KB
/
db.py
File metadata and controls
70 lines (61 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import psycopg2
from psycopg2.extras import RealDictCursor
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
# Connection for creating the DB
DB_HOST = "node128.codingbc.com"
DB_PORT = "9001"
DB_USER = "postgres"
DB_PASSWORD = "Lesson2017890"
DB_NAME = "yakovsand_invoice_db"
# SQLAlchemy connection string
SQLALCHEMY_DATABASE_URL = f"postgresql+psycopg2://{DB_USER}:{DB_PASSWORD}@{DB_HOST}:{DB_PORT}/{DB_NAME}"
engine = None
SessionLocal = None
def try_create_session():
global engine, SessionLocal
try:
engine = create_engine(SQLALCHEMY_DATABASE_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
print("Database connection successful.")
except Exception as e:
print(f"Error connecting to database with SQLAlchemy: {e}")
db_connection = None
def get_db_connection():
return get_db_connection_(DB_NAME)
def get_db_connection_(db_name):
global db_connection
try:
db_connection = psycopg2.connect(
host=DB_HOST,
port=DB_PORT,
user=DB_USER,
password=DB_PASSWORD,
dbname=db_name
)
db_connection.autocommit = True
return db_connection
except psycopg2.Error as e:
print(f"Error connecting to database '{db_name}': {e}")
db_connection = None
return None
def init_db():
conn = get_db_connection_('postgres') # connect to default 'postgres' DB
if conn:
try:
with conn.cursor() as cursor:
cursor.execute(f"CREATE DATABASE {DB_NAME};")
print(f"Database '{DB_NAME}' initialized successfully.")
return True
except psycopg2.Error as e:
if "already exists" in str(e):
print(f"Database '{DB_NAME}' already exists.")
return True
print(f"Error initializing database: {e}")
return False
finally:
conn.close()
return False
try_create_session()
if __name__ == "__main__":
init_db()