Skip to content
Snippets Groups Projects
Commit d4c12775 authored by Peter Morstein's avatar Peter Morstein
Browse files

update insert into statement

parent 3dbce1eb
No related branches found
No related tags found
No related merge requests found
[POSTGRES]
host = localhost
port = 5432
user = postgres
password = postgres
dbName = temperatures_berteld_morstein
[INTERPOLATION]
; neighbouring measurements used for interpolation
amount_neighbours = 5
; in km. 25km or 10km possible
matrix_density = 25
\ No newline at end of file
config.ini
......@@ -13,16 +13,18 @@ import psycopg2.extras
from psycopg2 import sql
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
import configparser
from api.GetAverageData import get_interpolation_data_for_point
#from api.GetAverageData import get_interpolation_data_for_point
cfg = configparser.ConfigParser()
cfg.read('../config.ini')
cfg.read('./config.ini')
assert "POSTGRES" in cfg, "missing POSTGRES in config.ini"
param_postgres = cfg["POSTGRES"]
param_interpol = cfg["INTERPOLATION"]
stationGPD = None
# get_average_data_for_point(52.5, 13.4)
# Create DB "temperatures_berteld_morstein"
def create_db(cursor):
print("Creating DB: ", param_postgres['dbName'])
......@@ -139,27 +141,40 @@ def create_matrix_data(cursor, amount_points):
# Dumping all existing data from database. Inserting station data into database in bulk.
def createInsertStatement(station_list):
# create INSERT INTO table (columns) VALUES('%s',...)
station_list.columns.astype(str)
df_columns = list(station_list)
station_list = station_list.round(decimals=3)
values = "VALUES({})".format(",".join(["%s" for _ in df_columns]))
# create INSERT INTO table (columns) VALUES('%s',...)
insert_stmt = """INSERT INTO {} ({}) {}""".format('stations', df_columns, values)
return insert_stmt
def insert_data(station_list, cursor):
print('Inserting data into database')
if len(station_list) > 0:
# print(stationList)
cursor.execute("DELETE FROM stations;")
df_columns = list(station_list)
#df_columns = list(station_list)
# create (col1,col2,...)
# As integers like 2018, 2017, etc. are not possible as column names, double quotes have to be added. This requires some tricks and cleanups
columns = ['"' + column + '"' for column in df_columns]
#columns = ['"' + column + '"' for column in df_columns]
# for column in df_columns:
# columns.append('"' + column + '"')
columns = str(columns).replace('[', '').replace(']', '').replace("'", "").replace('\n', '').replace(' ', '')
#columns = str(columns).replace('[', '').replace(']', '').replace("'", "").replace('\n', '').replace(' ', '')
station_list = station_list.round(decimals=3)
# create VALUES('%s', '%s",...) one '%s' per column
values = "VALUES({})".format(",".join(["%s" for _ in df_columns]))
#values = "VALUES({})".format(",".join(["%s" for _ in df_columns]))
# create INSERT INTO table (columns) VALUES('%s',...)
insert_stmt = """INSERT INTO {} ({}) {}""".format('stations', columns, values)
#insert_stmt = """INSERT INTO {} ({}) {}""".format('stations', columns, values)
insert_stmt = createInsertStatement(station_list)
psycopg2.extras.execute_batch(cursor, insert_stmt, station_list.values)
print('Done')
......
......@@ -4,28 +4,29 @@
Created on Wed Jul 14 13:45:28 2021
@author: geopeter
execute test from root
"""
import unittest
import pickle
import ExportToDatabase as cut
import dataacquisition.ExportToDatabase as cut
import configparser
import psycopg2
import time
cfg = configparser.ConfigParser()
cfg.read('../config.ini')
cfg.read('./config.ini')
assert "POSTGRES" in cfg, "missing POSTGRES in config.ini"
param_postgres = cfg["POSTGRES"]
stationList = None
testDB = 'weathertestdb'
class TestExportToDatabase(unittest.TestCase):
def testDBExists(self):
def _testDBExists(self):
print("__Test DB Exists")
#given
......@@ -38,7 +39,7 @@ class TestExportToDatabase(unittest.TestCase):
#finished
print("Test OKAY__")
def testCreateDB(self):
def _testCreateDB(self):
print("__Test CreateDB")
try:
# given
......@@ -56,7 +57,7 @@ class TestExportToDatabase(unittest.TestCase):
self.assertRaises(error)
print("Test FAILED__")
def testCreateTable(self):
def _testCreateTable(self):
print("__Test CreateTable")
try:
# given
......@@ -85,6 +86,20 @@ class TestExportToDatabase(unittest.TestCase):
connection.close()
self.assertRaises(error.message)
print("Test FAILED__")
def testCreateInsertStatement(self):
print("__Test CreateInsertStatement")
with open("./dataacquisition/pickle/stationList_with_temperature.pickle", "rb") as pickleFile:
stationList = pickle.load(pickleFile)
stationList = stationList.loc[stationList['country']=="Germany"]
insert_stmt = cut.createInsertStatement(stationList)
print(insert_stmt)
self.assertTrue(insert_stmt.startswith("INSERT INTO"))
self.assertTrue(insert_stmt.endswith("%s,%s)"))
if __name__ == '__main__':
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment