aboutsummaryrefslogtreecommitdiffstats
path: root/nb2pgsql.py
diff options
context:
space:
mode:
Diffstat (limited to 'nb2pgsql.py')
-rwxr-xr-xnb2pgsql.py31
1 files changed, 3 insertions, 28 deletions
diff --git a/nb2pgsql.py b/nb2pgsql.py
index 3993c41..1901182 100755
--- a/nb2pgsql.py
+++ b/nb2pgsql.py
@@ -4,7 +4,6 @@ import os
import sys
import csv
import json
-import psycopg2
import requests
import tempfile
@@ -12,6 +11,8 @@ from datetime import datetime
from datetime import timedelta
from tzlocal import get_localzone
+from common import dbi
+
# I'm not sure I understand Norges Banks json-model. It seems a lot easier to just get the CSV, and convert it to JSON.
apiUrl = "https://data.norges-bank.no/api/data/EXR/B.EUR.NOK.SP?format=csv&locale=en"
@@ -55,30 +56,4 @@ with open(temp.name) as csvfile:
temp.close()
-# connect to database
-conn = psycopg2.connect(database=pg_db, host=pg_host)
-cur = conn.cursor()
-
-# count rows before we start
-cur.execute("SELECT COUNT(*) FROM " + pg_table)
-before = cur.fetchone()
-
-# insert data
-try:
- cur.executemany("INSERT INTO " + pg_table + " VALUES(%s,%s,%s,%s) ON CONFLICT (startdate,base_cur,quote_cur) DO NOTHING", values)
- conn.commit()
-except Exception as e:
- conn.rollback()
- raise e
-
-# count rows after we finnish
-cur.execute("SELECT COUNT(*) FROM " + pg_table)
-after = cur.fetchone()
-
-# count *new* rows
-newRows = after[0] - before[0]
-
-# close connection
-conn.close()
-
-print("Successfully inserted " + str(newRows) + " records into the database")
+dbi("INSERT INTO " + pg_table + " VALUES(%s,%s,%s,%s) ON CONFLICT (startdate,base_cur,quote_cur) DO NOTHING", values,verbose=True)