Skip to content
Snippets Groups Projects
Commit a2b89b7a authored by Clemens Berteld's avatar Clemens Berteld
Browse files

API works and is finished (?)

parent 3b69b840
No related branches found
No related tags found
No related merge requests found
......@@ -204,6 +204,7 @@ def start():
# export station list to different outputs
#exportToWorldShape.export(stationList)
exportToDatabase.export(stationList)
print("___DWD Acquisition finished___")
......
......@@ -67,6 +67,7 @@ def insert_data(stationList):
with connection.cursor() as cursor:
if len(stationList) > 0:
# print(stationList)
cursor.execute("DELETE FROM stations;")
df_columns = list(stationList)
......@@ -77,7 +78,6 @@ def insert_data(stationList):
for column in df_columns:
columns.append('"' + column + '"')
columns = str(columns).replace('[', '').replace(']', '').replace("'", "").replace('\n', '').replace(' ', '')
stationList = stationList.round(decimals=3)
# create VALUES('%s', '%s",...) one '%s' per column
......
import json
import psycopg2
from flask import Flask, jsonify
from flask import Flask, jsonify, request
from psycopg2 import sql
app = Flask(__name__)
......@@ -11,13 +11,37 @@ app.config['DEBUG'] = True
app.config['TESTING'] = False
@app.route('/')
@app.route('/', methods=['GET'])
def index():
wheres = sql.SQL('')
values = '' # Gets used in cursor.execute()s' second parameter for safety reasons
if 'id' in request.args:
station_id = request.args['id']
wheres = wheres + (sql.SQL("AND id = {values} ").format(column=sql.Identifier('stations', 'id'), values=sql.Placeholder()))
for n in [int(station_id)]:
values = (*values, n) # adding n to existing tuple
# Just for development
# if 'country' in request.args:
# country = request.args['country']
# wheres = wheres + (sql.SQL("AND LOWER({column}) LIKE {values} ").format(column=sql.Identifier('stations', 'country'), values=sql.Placeholder()))
# for n in [country]:
# values = (*values, n) # adding n to existing tuple
query = sql.SQL("SELECT array_to_json(array_agg(row_to_json(t))) from ("
"SELECT * FROM stations WHERE lon IS NOT NULL "
"{} "
") t;").format(wheres)
with psycopg2.connect(database='temperatures_berteld_morstein', user='postgres', password='postgres', host='localhost', port=5432) as connection:
with connection.cursor() as cursor:
# query = sql.SQL("""CREATE TABLE stations ({});""".format(columns_clean))
pass
return jsonify({'läuft': 'jo'})
print(query.as_string(cursor))
print(values)
cursor.execute(query, values)
results = cursor.fetchall()[0][0]
return jsonify(results)
app.run(host='127.0.0.1', port=42000)
import psycopg2
from psycopg2 import sql
query = sql.SQL("select {0} from {1} where {2} LIKE {3} {4}").format(sql.SQL(', ').join([sql.Identifier('foo'), sql.Identifier('bar')]), sql.Identifier('table'), sql.Identifier('coulumn'), sql.Placeholder(),
sql.SQL('AND 1 = 1'))
with psycopg2.connect(database='temperatures_berteld_morstein', user='postgres', password='postgres', host='localhost', port=5432) as connection:
with connection.cursor() as cursor:
print(query.as_string(cursor))
for i in range(1949, 2019):
print(('"{}" NUMERIC,').format(str(i)))
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