Some working db writer code.
This commit is contained in:
@@ -1,16 +1,15 @@
|
||||
#! env/bin/python3
|
||||
|
||||
# import the psycopg2 database adapter for PostgreSQL
|
||||
from psycopg2 import connect, Error
|
||||
import json
|
||||
import psycopg2
|
||||
from psycopg2.extras import Json
|
||||
from psycopg2.extras import json as psycop_json
|
||||
import json
|
||||
import sys
|
||||
|
||||
def connect_db(db: str,host: str,user: str,passwd: str):
|
||||
try:
|
||||
# declare a new PostgreSQL connection object
|
||||
conn = connect(
|
||||
conn = psycopg2.connect(
|
||||
dbname = db,
|
||||
user = user,
|
||||
host = host,
|
||||
@@ -18,86 +17,57 @@ def connect_db(db: str,host: str,user: str,passwd: str):
|
||||
# attempt to connect for 3 seconds then raise exception
|
||||
connect_timeout = 3
|
||||
)
|
||||
cur = conn.cursor()
|
||||
|
||||
except (Exception, Error) as err:
|
||||
except (Exception, psycopg2.Error) as err:
|
||||
#print ("\npsycopg2 connect error:", err)
|
||||
#conn = None
|
||||
cur = None
|
||||
return cur
|
||||
conn = None
|
||||
return conn
|
||||
|
||||
# use Python's open() function to load the JSON data
|
||||
with open('postgres-records.json') as json_data:
|
||||
|
||||
# use load() rather than loads() for JSON files
|
||||
record_list = json.load(json_data)
|
||||
|
||||
# concatenate an SQL string
|
||||
sql_string = 'INSERT INTO {} '.format( table_name )
|
||||
|
||||
# if record list then get column names from first key
|
||||
if type(record_list) == list:
|
||||
first_record = record_list[0]
|
||||
|
||||
columns = list(first_record.keys())
|
||||
print ("\ncolumn names:", columns)
|
||||
|
||||
# if just one dict obj or nested JSON dict
|
||||
else:
|
||||
print ("Needs to be an array of JSON objects")
|
||||
sys.exit()
|
||||
|
||||
# enclose the column names within parenthesis
|
||||
sql_string += "(" + ', '.join(columns) + ")\nVALUES "
|
||||
|
||||
# only attempt to execute SQL if cursor is valid
|
||||
if cur != None:
|
||||
def get_db_creds(file: str):
|
||||
with open(file) as cred_file:
|
||||
creds = json.load(cred_file)
|
||||
return creds
|
||||
|
||||
def insert_data(conn, data):
|
||||
|
||||
# insert a new vendor into the vendors table
|
||||
sql = """
|
||||
INSERT INTO
|
||||
air(datetime, temperature, humidity)
|
||||
VALUES
|
||||
(%s, %s, %s)
|
||||
"""
|
||||
|
||||
try:
|
||||
cur.execute( sql_string )
|
||||
conn.commit()
|
||||
|
||||
print ('\nfinished INSERT INTO execution')
|
||||
|
||||
except (Exception, Error) as error:
|
||||
print("\nexecute_sql() error:", error)
|
||||
conn.rollback()
|
||||
|
||||
# close the cursor and connection
|
||||
cur.close()
|
||||
conn.close()
|
||||
|
||||
######################################################3
|
||||
|
||||
import psycopg2
|
||||
from config import config
|
||||
|
||||
|
||||
def insert_vendor(vendor_name):
|
||||
""" insert a new vendor into the vendors table """
|
||||
sql = """INSERT INTO vendors(vendor_name)
|
||||
VALUES(%s) RETURNING vendor_id;"""
|
||||
conn = None
|
||||
vendor_id = None
|
||||
try:
|
||||
# read database configuration
|
||||
params = config()
|
||||
# connect to the PostgreSQL database
|
||||
conn = psycopg2.connect(**params)
|
||||
# create a new cursor
|
||||
# open cursor on our db connection
|
||||
cur = conn.cursor()
|
||||
|
||||
# execute the INSERT statement
|
||||
cur.execute(sql, (vendor_name,))
|
||||
# get the generated id back
|
||||
vendor_id = cur.fetchone()[0]
|
||||
data = (data["datetime"], data["temperature"], data["humidity"])
|
||||
cur.execute(sql,data)
|
||||
|
||||
# commit the changes to the database
|
||||
conn.commit()
|
||||
|
||||
# close communication with the database
|
||||
cur.close()
|
||||
|
||||
except (Exception, psycopg2.DatabaseError) as error:
|
||||
print(error)
|
||||
|
||||
finally:
|
||||
if conn is not None:
|
||||
conn.close()
|
||||
|
||||
if __name__ == "__main__":
|
||||
data = {
|
||||
"datetime": "2021-10-23 01:58:08.205911",
|
||||
"temperature": "73.4",
|
||||
"humidity": "49.2"
|
||||
}
|
||||
|
||||
return vendor_id
|
||||
creds = get_db_creds("./.creds.json")
|
||||
|
||||
conn = connect_db(creds["db"], creds["host"], creds["user"], creds["passwd"])
|
||||
|
||||
insert_data(conn, data)
|
||||
Reference in New Issue
Block a user