2

It looks like similar questions have been asked but I didn't find an answer.

The cities in the cities list have corresponding column names in a database. I'm trying to find a specific city's pressure with pyowm, then insert that value into the appropriate column.

The error is that there is not a column named "city". I can see what the problem is but do not know how to fix it. Any help is greatly appreciated!

import pyowm
import sqlite3

conn = sqlite3.connect(r"C:\Users\Hanley Smith\Desktop\machinelearning\pressure_table.db")
cursor = conn.cursor()

owm = pyowm.OWM('eb68e3b0c908251771e67882d7a8ddff')

cities = ["tokyo", "jakarta"]

for city in cities:
    weather = owm.weather_at_place(city).get_weather()
    pressure = weather.get_pressure()['press']
    cursor.execute("INSERT INTO PRESSURE (city) values (?)", (pressure,))

conn.commit()
2
  • Are you just trying to use a variable as the column name, or the value? Commented Oct 5, 2016 at 2:51
  • @StevenByrne the variable for the column name should be "city" from the "cities" list. The variable for the value should be "pressure" as defined in the for-loop Commented Oct 5, 2016 at 3:22

1 Answer 1

2

concatenate the variable name with the string like this.

cursor.execute("INSERT INTO PRESSURE (" + city + ") values (?)", (pressure,))

or a much cleaner way with %s

cursor.execute("INSERT INTO PRESSURE (%s) values (?)" % (city), (pressure,))
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.