This repository has been archived by the owner on Jul 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
cockroachScripts.py
119 lines (105 loc) · 3.7 KB
/
cockroachScripts.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
from dotenv import load_dotenv
import os, time, random, logging, psycopg2, psycopg2.extras
from argparse import ArgumentParser, RawTextHelpFormatter
from psycopg2.errors import SerializationFailure
load_dotenv()
""" #loading password
import json
with open("secret.json") as f:
secret = json.load(f)
# with secrets.json
def checkConnection():
#connection with database
try:
conn = psycopg2.connect(user=secret['DB_USER'],
password=secret['DB_PASS'],
host=secret['DB_HOST'],
port=secret['DB_PORT'],
database=secret['DB_NAME'])
print("Connected to SMU DB")
return conn
except (Exception, psycopg2.Error) as error:
print("Failed to complete request", error)
return False """
# with env file
def checkConnection():
#connection with database
try:
conn = psycopg2.connect(user=os.getenv('DB_USER'),
password=os.getenv('DB_PASS'),
host=os.getenv('DB_HOST'),
port=os.getenv('DB_PORT'),
database=os.getenv('DB_NAME'))
cur = conn.cursor()
print("Connected to SMU DB")
return conn
except (Exception, psycopg2.Error) as error:
print("Failed to complete request", error)
return False
def closeConnection(conn):
#closing database connection.
if conn:
conn.close()
print("PostgreSQL connection is closed")
def createTableSubscribers(sid):
conn = checkConnection()
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS _''' + str(sid) + '''_subscribers(
id BIGSERIAL NOT NULL PRIMARY KEY,
email VARCHAR(150) NOT NULL);''')
conn.commit()
closeConnection(conn)
def createTableTemplates(sid):
conn = checkConnection()
cur = conn.cursor()
cur.execute('''CREATE TABLE IF NOT EXISTS _''' + str(sid) + '''_templates(
id BIGSERIAL NOT NULL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
link VARCHAR(50) NOT NULL);''')
conn.commit()
closeConnection(conn)
def addDataTemplates(sid, name, link):
conn = checkConnection()
cur = conn.cursor()
cur.execute('''INSERT INTO _''' + str(sid) + '''_templates(name, link)
VALUES('{0}','{1}');'''.format(name, link))
conn.commit()
closeConnection(conn)
def addDataSubscribers(sid, email):
conn = checkConnection()
cur = conn.cursor()
cur.execute('''INSERT INTO _''' + str(sid) + '''_subscribers(email)
VALUES('{0}');'''.format(email))
conn.commit()
closeConnection(conn)
def readDataTemplates(sid):
conn = checkConnection()
cur = conn.cursor()
tableName = "_" + str(sid) + "_templates"
cur.execute("SELECT * FROM " + tableName)
cur.execute("SELECT COUNT (*) FROM " + tableName)
count = cur.fetchone()
cur.execute("SELECT * FROM " + tableName)
return cur.fetchall() # returns tuple
conn.commit()
closeConnection(conn)
def readDataSubscribers(sid):
conn = checkConnection()
cur = conn.cursor()
tableName = "_" + str(sid) + "_subscribers"
cur.execute("SELECT * FROM " + tableName)
cur.execute("SELECT COUNT (*) FROM " + tableName)
count = cur.fetchone()
cur.execute("SELECT * FROM " + tableName)
return cur.fetchall()
conn.commit()
closeConnection(conn)
#for testing
if __name__ == '__main__':
""" checkConnection()
createTableTemplates(50)
createTableSubscribers(50)
addDataTemplates(50, 'dec', 'blue')
addDataSubscribers(50, '[email protected]')
readDataTemplates(52)
readDataSubscribers(52) """