Team Ai
Apppublic

ahmad-sobeh/fastapi-sqlite-crud

sourceHugging Facemitupdated 5mo agoView on Hugging Face
0likes
testing.py33 linesDownload Raw Back to root
1import os
2import psycopg2
3from psycopg2.extras import RealDictCursor
4from dotenv import load_dotenv
5
6load_dotenv()  # reads .env file automatically
7
8DATABASE_URL = os.environ.get("DATABASE_URL")
9
10def get_conn():
11    return psycopg2.connect(DATABASE_URL, cursor_factory=RealDictCursor)
12
13def test():
14    try:
15        DATABASE_URL = os.environ.get("DATABASE_URL")
16        print("DB URL:", DATABASE_URL)  # should NOT print None
17
18        conn = get_conn()
19        cur  = conn.cursor()
20        cur.execute(
21        "INSERT INTO agent_logs (agent_name, question, answer) VALUES (%s, %s, %s)",
22        ("agent_name", "question", "answer")
23                    )
24        conn.commit()
25        cur.close()
26        conn.close()
27        print('done saved')
28    except Exception as e:
29        print(str(e))
30        return str(e)
31    
32if __name__ == "__main__":
33    test()