Hi,
I am using system.net.httpGet() to fetch weather data and writing it to a table in SQL server. Manage to get it working by inserting the data received 1 by 1, but i cannot figure it out to do it with just 1 insert. Any idea how to do it? It is even possible?
The below code works for me:
import json
# get the json weather response from the NOAA.
lat = "38.6524"
lng = "-121.1896"
url = "https://api.weather.gov/points/%s,%s" % (lat, lng)
noaaResponse = system.net.httpGet(url)
noaaJSON = system.util.jsonDecode(noaaResponse)
# Find the forecast URL.
properties = noaaJSON["properties"]
forecastURL = properties["forecast"]
# Get the forecast from NOAA.
forecastResponse = system.net.httpGet(forecastURL)
forecastJSON = system.util.jsonDecode(forecastResponse)
# Print out the forecast in a human-readable way.
periods = forecastJSON["properties"]["periods"]
# Define the SQL query to insert the data into the table
sql = "INSERT INTO weather_forecast (period_name, temperature, detailed_forecast) VALUES (?, ?, ?)"
#Convert JSON to string if you want to save it in a column later. OPTIONAL.
#jsonstring = json.dumps(forecastJSON)
#Loop and insert rows 1 by 1
for data in periods:
period_name = data["name"]
temperature = str(data["temperature"]) + " °F"
detailed_forecast = data["detailedForecast"]
args = [period_name, temperature, detailed_forecast]
system.db.runPrepUpdate(sql, args, "SQLConnection")