-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase_tools.py
More file actions
46 lines (43 loc) · 1.45 KB
/
database_tools.py
File metadata and controls
46 lines (43 loc) · 1.45 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
import psycopg2
from config import POSTGRES_CONFIG, NEBULA_ID
def connect_to_db():
try:
conn = psycopg2.connect(**POSTGRES_CONFIG)
print("Connection successful!")
conn.close()
except Exception as e:
print(f"Connection failed: {e}")
def check_if_user_exists(steam_id):
try:
connection = psycopg2.connect(**POSTGRES_CONFIG)
cursor = connection.cursor()
cursor.execute("SELECT * FROM users WHERE steamid=%s", (steam_id,))
result = cursor.fetchone()
if result:
print(f"User with ID {steam_id} found in DB!")
return (True, result[1])
print(f"User with ID {steam_id} not found in DB.")
return (False, None)
except Exception as e:
print(f"Error: {e}")
return (False, None)
finally:
cursor.close()
connection.close()
def save_user_to_db(steam_id, tags):
try:
connection = psycopg2.connect(**POSTGRES_CONFIG)
cursor = connection.cursor()
cursor.execute("""
INSERT INTO users (steamid, weighted_tags)
VALUES (%s, %s)
ON CONFLICT (steamid)
DO UPDATE SET weighted_tags = EXCLUDED.weighted_tags;
""", (steam_id, tags))
connection.commit()
print(f"User with ID {steam_id} saved to database.")
except Exception as e:
print(f"Error: {e}")
finally:
cursor.close()
connection.close()