-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdbtest.py
39 lines (23 loc) · 916 Bytes
/
dbtest.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
import os
import psycopg2
from psycopg2 import errors
from dotenv import load_dotenv
# Load environment variables from the .env file
load_dotenv()
# Retrieve Postgres database credentials from environment variables
host = os.environ.get('POSTGRES_HOST')
dbname = os.environ.get('POSTGRES_DBNAME')
user = os.environ.get('POSTGRES_USER')
password = os.environ.get('POSTGRES_PASSWORD')
port = os.environ.get('POSTGRES_PORT')
conn = psycopg2.connect(host=host, dbname=dbname, user=user, password=password, port=port)
cur = conn.cursor()
# swd_imei="01234567890"
# q = "INSERT INTO things (swd_imei, lastupdated, firstseen) VALUES ('" +swd_imei +"', NOW(), NOW())"
# cur.execute(q)
# r = conn.commit()
# print(r)
q = "INSERT INTO mqtt (timestamp, message, imei, topic, payload, crc) VALUES (NOW(), 'testmessage', '01234567890' , 'test','testpayload', '99999')"
r=cur.execute(q)
print(r)
conn.commit()