-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.py
40 lines (32 loc) · 826 Bytes
/
task.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
import psycopg2
import os
from dotenv import load_dotenv
load_dotenv()
conn = psycopg2.connect(
host=os.getenv("host"),
user=os.getenv("username"),
database=os.getenv("database"),
password=os.getenv("password"),
port=os.getenv("port")
)
cur = conn.cursor()
cur.execute("""
CREATE TABLE IF NOT EXISTS task (
id BIGSERIAL PRIMARY KEY,
name VARCHAR(50) UNIQUE NOT NULL,
isActive BOOLEAN DEFAULT FALSE
);
""")
conn.commit()
try:
fetch_query = "SELECT * FROM task WHERE isActive = TRUE;"
cur.execute(fetch_query)
active_tasks = cur.fetchall()
print(f"Number of active tasks: {len(active_tasks)}")
except Exception as e:
print(f"An error occurred: {e}")
finally:
if cur is not None:
cur.close()
if conn is not None:
conn.close()