-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgre.py
More file actions
162 lines (144 loc) · 4.77 KB
/
postgre.py
File metadata and controls
162 lines (144 loc) · 4.77 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
import psycopg2
def createConnection():
try:
if os.getenv("PRJ_EXECUTER") == "heroku":
connection = psycopg2.connect(
os.environ['DATABASE_URL'], sslmode='require')
else:
connection = psycopg2.connect(host=os.getenv("postgres_host"),
database=os.getenv(
"postgres_database"),
user=os.getenv("postgres_user"),
password=os.getenv("postgres_password"))
return connection
except:
print("Error: Could not connect to the database")
return None
def rowExist(connection, table, nameOfRow, row):
cur = connection.cursor()
cur.execute(
"SELECT " + nameOfRow + " FROM "+table+" WHERE "+nameOfRow+" = %s", (row,))
return cur.fetchone() is not None
def tableCheck(connection, table):
print("Enter tableCheck")
if connection is None:
return None
cursor = connection.cursor()
# Check if results table exists, if not create it
""" cursor.execute(
"select exists(select * from information_schema.tables where table_name= %s)", (table,))
doExist = cursor.fetchone()[0] """
try:
cursor.execute("select * from " + table)
doExist = True
except:
doExist = False
cursor.close()
connection.commit()
cursor = connection.cursor()
print(doExist)
if doExist:
print("Table exists")
return True
else:
if table == 'results':
cursor.execute(
"CREATE TABLE results (id serial PRIMARY KEY, hashtag varchar(255), data TEXT)")
print("Table created")
elif table == 'hashtagList':
cursor.execute(
"CREATE TABLE hashtagList (id serial PRIMARY KEY, hashtag varchar(255))")
print("Table created")
cursor.close()
connection.commit()
return True
def saveData(data, hashtag):
print("Enter saveData")
connection = createConnection()
if connection is None:
return None
checkExistence = tableCheck(connection, 'results')
cursor = connection.cursor()
# save data to results table
# Check if row actually exist
print("hashtag: " + hashtag)
doRowExist = rowExist(connection, "results", "hashtag", hashtag)
print("rowExist: " + str(doRowExist))
if doRowExist:
cursor.execute(
"UPDATE results SET data=%s WHERE hashtag=%s", (data, hashtag))
else:
cursor.execute(
"INSERT INTO results (hashtag, data) VALUES (%s, %s)", (hashtag, data))
cursor.close()
connection.commit()
connection.close()
return True
def fetchData(hashtag):
print("Enter fetchData")
connection = createConnection()
if connection is None:
return None
checkExistence = tableCheck(connection, 'results')
cursor = connection.cursor()
# fetch data from results table
cursor.execute(
"SELECT data FROM results WHERE hashtag = %s", (hashtag,))
data = cursor.fetchall()
cursor.close()
connection.close()
return data
def getHashtagList():
print("Enter getHashtagList")
connection = createConnection()
if connection is None:
return None
checkExistence = tableCheck(connection, 'hashtagList')
cursor = connection.cursor()
# fetch data from results table
cursor.execute(
"SELECT hashtag FROM hashtagList")
data = cursor.fetchall()
cursor.close()
connection.close()
return data
def findHashtag(hashtag):
print("Enter findHashtag")
connection = createConnection()
if connection is None:
return None
checkExistence = tableCheck(connection, 'hashtagList')
cursor = connection.cursor()
cursor.execute(
"SELECT hashtag FROM hashtagList WHERE hashtag = %s", (hashtag,))
data = cursor.fetchone()
cursor.close()
connection.close()
return data
def removeHashtag(hashtag):
print("Enter removeHashtag")
connection = createConnection()
if connection is None:
return None
checkExistence = tableCheck(connection, 'hashtagList')
cursor = connection.cursor()
cursor.execute(
"DELETE FROM hashtagList WHERE hashtag = %s", (hashtag,))
cursor.close()
connection.commit()
connection.close()
return True
def savehashtagList(hashtagList):
print("Enter savehashtagList")
connection = createConnection()
if connection is None:
return None
checkExistence = tableCheck(connection, 'hashtagList')
cursor = connection.cursor()
cursor.execute(
"INSERT INTO hashtagList (hashtag) VALUES (%s)", (hashtagList,))
cursor.close()
connection.commit()
connection.close()
return True