-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostgres_conn.py
executable file
·59 lines (50 loc) · 1.58 KB
/
postgres_conn.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
import os
import psycopg2
from threading import Thread
# Table name: Jar
# string userid [PRIMARY KEY], string username, string realname, int amount
DATABASE_URL = os.environ['HEROKU_POSTGRESQL_JADE_URL']
class Postgres(object):
def __init__(self):
self.conn = psycopg2.connect(DATABASE_URL)
self.cur = self.conn.cursor()
self.update_interval = 15
def incrementSwearCount(self, user, swearIncrement):
swearcount = self.getSwearCount(user)
finalcount = swearcount + swearIncrement
update_swear_query = "update swearjar set swearcount = %s where userid = %s"
try:
self.cur.execute(update_swear_query, (finalcount, user))
self.conn.commit()
except psycopg2.Error as e:
print(e.pgerror)
return finalcount
def getSwearCount(self, user):
return self.getUserData(user)[3]
def getUserData(self, user):
find_query = "select * from swearjar where userid = %s"
try:
self.cur.execute(find_query, (user,))
user_data = self.cur.fetchone()
return user_data
except psycopg2.Error as e:
print(e.pgerror)
def getAllUserSwearCounts(self):
lookup_query = "select userid, username, swearcount from swearjar"
try:
self.cur.execute(lookup_query, ())
all_user_swearcounts = self.cur.fetchall()
return all_user_swearcounts
except psycopg2.Error as e:
print(e.pgerror)
def addNewUser(self, user_info):
add_user_query = "insert into swearjar VALUES(%s, %s, %s, %s)"
try:
self.cur.execute(add_user_query, (
user_info["id"],
user_info["name"],
user_info["real_name"],
0))
self.conn.commit()
except psycopg2.Error as e:
print(e.pgerror)