-
Notifications
You must be signed in to change notification settings - Fork 3
/
databaseTemplate.py
41 lines (29 loc) · 1.25 KB
/
databaseTemplate.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
import json
import psycopg2
import datetime
timeFormat = '%Y-%m-%d %H:%M:%S'
now = datetime.datetime.now()
with open('config/db.json') as data_file:
cfg = json.load(data_file)
username = cfg['username']
password = cfg['password']
host = cfg['host']
databasename = cfg['databasename']
#cnxn = pyodbc.connect('DRIVER={psycopg2};SERVER=' + host + ';DATABASE=' + databasename + ';UID=' + username + ';PWD=' + password)
conn = psycopg2.connect(host = host, user = username, password = password, dbname = databasename)
cur = conn.cursor()
# Execute a command: this creates a new table
#cur.execute("CREATE TABLE Arduino1 (id serial PRIMARY KEY, time TIMESTAMP, thermistor1 float, phSensor1 float);")
#cur.execute("DROP TABLE Arduino1;")
#cur.execute("ALTER TABLE Arduino1 ADD COLUMN conductivity1 float;")
# Pass data to fill a query placeholders and let Psycopg perform
# the correct conversion (no more SQL injections!)
#cur.execute("INSERT INTO Thermistor1 (data, time) VALUES (%s, %s)", (13.2, now.strftime(timeFormat)))
# Query the database and obtain data as Python objects
#cur.execute("SELECT * FROM Arduino1;")
#print(cur.fetchall())
# Make the changes to the database persistent
conn.commit()
# Close communication with the database
cur.close()
conn=conn.close()