-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpostgresql_storage.py
61 lines (53 loc) · 1.75 KB
/
postgresql_storage.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
import psycopg2
import datetime
import os
class SQLAffiliate:
def __init__(self):
self.db_path = {
"dbname": "railway",
"user": os.getenv("PGUSER"),
"password": os.getenv("PGPASSWORD"),
"port": "5432",
"host": os.getenv("PGHOST"),
}
def initialize_db(self):
db = psycopg2.connect(**self.db_path)
cursor = db.cursor()
cursor.execute(
"CREATE TABLE IF NOT EXISTS users (id SERIAL PRIMARY KEY, uid TEXT, approvalDatetime TEXT, username TEXT, deposit INTEGER, exchange TEXT);"
)
db.commit()
db.close()
def add_user(self, uid, username, deposit, exchange):
db = psycopg2.connect(**self.db_path)
now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
cursor = db.cursor()
uid = str(uid)
now = str(now)
cursor.execute(
"INSERT INTO users (uid, approvalDatetime, username, deposit, exchange) VALUES (%s,%s,%s,%s,%s);",
(uid, now, username, deposit, exchange,),
)
db.commit()
db.close()
def check_user_exists(self, uid):
db = psycopg2.connect(**self.db_path)
cursor = db.cursor()
uid = str(uid)
cursor.execute("SELECT * FROM users WHERE uid=%s ;", (uid,))
user = cursor.fetchone()
db.close()
if user:
return True
return False
def get_users(self):
db = psycopg2.connect(**self.db_path)
cursor = db.cursor()
cursor.execute("SELECT * FROM users;")
users = cursor.fetchall()
db.close()
return users
if __name__ == "__main__":
sql_db = SQLAffiliate()
print(sql_db.initialize_db())
print(sql_db.get_users())