didier/functions/database/utils.py
Stijn De Clercq f1138c3b56 Initial commit
Transfer Didier to this repo
2020-10-13 21:06:00 +02:00

36 lines
640 B
Python

import psycopg2
import json
import os
connection = None
def connect():
global connection
if connection is None:
create_connection()
return connection
def create_connection():
global connection
dir_path = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(dir_path, "../../files/database.json"), "r") as fp:
db = json.load(fp)
connection = psycopg2.connect(
host=db["host"],
database=db["database"],
user=db["username"],
password=db["password"]
)
def reconnect():
global connection
connection.close()
create_connection()