-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
48 lines (42 loc) · 963 Bytes
/
db.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
import psycopg2
from dotenv import load_dotenv
load_dotenv()
import os
conn = None
def connect():
global conn
conn = psycopg2.connect(
host=os.getenv('DB_HOST'),
user=os.getenv('DB_USER'),
password=os.getenv('DB_PASSWORD'),
database =os.getenv('DB_NAME'),
)
conn.autocommit = True
return True
def disconnect():
global conn
conn.close()
return True
def create():
cur = conn.cursor()
q = '''CREATE TABLE IF NOT EXISTS tempusers(
id SERIAL PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
token VARCHAR(255) NOT NULL,
encodings TEXT NOT NULL
)'''
cur.execute(q)
cur.close()
return True
def select(query):
cur = conn.cursor()
cur.execute(query)
rows = cur.fetchall()
cur.close()
return rows
def insert(query):
cur = conn.cursor()
cur.execute(query)
cur.close()
return True