diff --git a/db.py b/db.py index 424cae4..2a77d71 100644 --- a/db.py +++ b/db.py @@ -1,5 +1,7 @@ #!/usr/bin/python import psycopg2 +import csv +import os def connect_db(db_settings): conn = None @@ -44,6 +46,25 @@ def add_history_entry(db_settings, itemid, skuid, choice, attributes, price, cur connection.close() +def export_csv(db_settings): + connection = connect_db(db_settings) + cursor = connection.cursor() + + cursor.execute(""" + SELECT i.itemid, i.skuid, i.choice, i.attributes, h.quantity, h.discount_percentage, h.price, h.currency, h.h_timestamp + FROM item i, history h + WHERE i.itemid = h.itemid and i.skuid = h.skuid + """) + results = cursor.fetchall() + with open(os.path.dirname(os.path.realpath(__file__))+"/output.csv", 'w') as csv_file: + # Create a CSV writer + writer = csv.writer(csv_file) + # write the column names + writer.writerow([col[0] for col in cursor.description]) + # write the query results + writer.writerows(results) + cursor.close() + connection.close() def initialize(db_settings): diff --git a/main.py b/main.py index 1d17b3f..efb0884 100644 --- a/main.py +++ b/main.py @@ -17,4 +17,5 @@ if __name__ == '__main__': #initialize(settings["db"]) fill_db(settings["db"], check_items(settings["item"])) + export_csv(settings["db"]) diff --git a/web/test.html b/web/test.html new file mode 100644 index 0000000..7310b94 --- /dev/null +++ b/web/test.html @@ -0,0 +1,116 @@ + + + + + + + +
+ +