Files
very-bad-bot/src/helper/db_helper.py

45 lines
1.0 KiB
Python

"""
database interaction module of the very-bad bot
@author: Gabriella 'contrastellar' Agathon
"""
import psycopg2
import psycopg2.extensions
from configparser import ConfigParser
import datetime
def load_config(filename='database.ini', section='postgresql'):
parser = ConfigParser()
parser.read(filename)
# get section, default is postgresql
config = {}
if parser.has_section(section):
params = parser.items(section)
for param in params:
config[param[0]] = param[1]
else:
raise Exception('Section {0} not found in the {1} file'.format(section, filename))
return config
def connect_config(config) -> psycopg2.extensions.connection:
try:
with psycopg2.connect(**config) as conn:
print('connected to the server')
return conn
except (psycopg2.DatabaseError, Exception) as error:
print(error)
finally:
if conn is None:
raise psycopg2.DatabaseError("Failed to connect!")