51 lines
1.5 KiB
Python
51 lines
1.5 KiB
Python
import psycopg2
|
|
|
|
def printNotice(dbConnection, i):
|
|
print("(DB HANDLER) " + dbConnection.notices[i])
|
|
|
|
def connect(**options):
|
|
return psycopg2.connect(database=options["database"],
|
|
host=options["host"],
|
|
user=options["user"],
|
|
password=options["password"],
|
|
port=options["port"])
|
|
|
|
def initTable(dbConnection, tableName, tableFormat):
|
|
dbCursor = dbConnection.cursor()
|
|
|
|
dbCursor.execute("""
|
|
DO $$
|
|
BEGIN
|
|
IF (EXISTS (SELECT *
|
|
FROM INFORMATION_SCHEMA.TABLES
|
|
WHERE TABLE_NAME = '""" + tableName.lower() + """'))
|
|
THEN
|
|
RAISE NOTICE '""" + tableName + """ Table does exist! Skipping creating table.';
|
|
ELSE
|
|
RAISE NOTICE '""" + tableName + """ Table does not exist! Creating table.';
|
|
CREATE TABLE """ + tableName.lower() + """ (
|
|
""" + tableFormat + """
|
|
);
|
|
END IF;
|
|
END;
|
|
$$""")
|
|
printNotice(dbConnection, -1)
|
|
|
|
dbConnection.commit()
|
|
dbCursor.close()
|
|
|
|
def commitQuery(dbConnection, query):
|
|
dbCursor = dbConnection.cursor()
|
|
dbCursor.execute(query)
|
|
dbConnection.commit()
|
|
dbResults = dbCursor.fetchall()
|
|
dbCursor.close()
|
|
return dbResults
|
|
|
|
def execQuery(dbConnection, query):
|
|
dbCursor = dbConnection.cursor()
|
|
dbCursor.execute(query)
|
|
dbResults = dbCursor.fetchall()
|
|
dbCursor.close()
|
|
return dbResults
|