-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_functions.py
70 lines (66 loc) · 2.32 KB
/
db_functions.py
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 # PostgreSQL functionality
from config import config # Read database connection info from db.ini
__all__ = ["new_row", "create_table", "check_table"]
def new_row(query):
conn = None
try:
# Read the connection parameters
params = config()
# Connect to the PostgreSQL server
conn = psycopg2.connect(**params)
cur = conn.cursor()
# Create new row
cur.execute(query)
# Close communication with the PostgreSQL database server
cur.close()
# Commit the changes
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def create_table(name, query):
"""create a table in the PostgreSQL database"""
conn = None
try:
# read the connection parameters
params = config()
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
cur = conn.cursor()
# check if table exists - returns true or false
cur.execute(f"select exists(select * from information_schema.tables where table_name='{name}')")
table_exists = cur.fetchone()[0]
# create the table if it doesn't exist
if table_exists != True:
cur.execute(query)
# close communication with the PostgreSQL database server
cur.close()
# commit the changes
conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()
def check_table(value, column, table):
"""check if value exists in column in table - returns True or False"""
conn = None
try:
# read the connection parameters
params = config()
# connect to the PostgreSQL server
conn = psycopg2.connect(**params)
cur = conn.cursor()
# check table for value
cur.execute(f"select exists(select {column} from {table} where {column} = {value})")
value_exists = cur.fetchone()[0]
# close communication with the PostgreSQL database server
cur.close()
return value_exists
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if conn is not None:
conn.close()