0

I have a tuple as below

data = ({'weather station name': 'Substation', 'wind': '6 km/h', 'barometer': '1010.3hPa', 'humidity': '42%', 'temperature': '34.5 C', 'place_id': '001D0A00B36E', 'date': '2016-05-10 09:48:58'})

I am trying to push the values from the above tuple to the postgres table using the code below:

try:                                                                                                                                                                                                         
    con = psycopg2.connect("dbname='WeatherForecast' user='postgres' host='localhost' password='****'")                                                                                                         
    cur = con.cursor()                                                                                                                                                                                                  
    cur.executemany("""INSERT INTO weather_data(temperature,humidity,wind,barometer,updated_on,place_id) VALUES (%(temperature)f, %(humidity)f, %(wind)f, %(barometer)f, %(date)s, %(place_id)d)""", final_weather_data)
    ver = cur.fetchone()                                                                                                                                                                                                
    print(ver)                                                                                                                                                                                                          

except psycopg2.DatabaseError as e:                                                                                                                                                                                     
    print('Error {}'.format(e))                                                                                                                                                                                         
    sys.exit(1)

finally:                                                                                                                                                                                                                

  if con:                                                                                                                                                                                                             
     con.close()  

Where datatype of each field in the DB is as below: id serial NOT NULL,

temperature double precision NOT NULL,
humidity double precision NOT NULL,
wind double precision NOT NULL,
barometer double precision NOT NULL,
updated_on timestamp with time zone NOT NULL,
place_id integer NOT NULL,  

When i run the code to push the data into postgres table using psycopg2, it is raising an error "ValueError: unsupported format character 'f'"

I hope the issue is in formatting. Am using Python3.4

1 Answer 1

2

Have a look at the documentation:

The variables placeholder must always be a %s, even if a different placeholder (such as a %d for integers or %f for floats) may look more appropriate:

>>> cur.execute("INSERT INTO numbers VALUES (%d)", (42,)) # WRONG
>>> cur.execute("INSERT INTO numbers VALUES (%s)", (42,)) # correct

While, your SQL query contains all type of placeholders:

"""INSERT INTO weather_data(temperature,humidity,wind,barometer,updated_on,place_id) 
   VALUES (%(temperature)f, %(humidity)f, %(wind)f, %(barometer)f, %(date)s, %(place_id)d)"""
Sign up to request clarification or add additional context in comments.

3 Comments

I tried changing all the placeholder to %s. But am receiving TypeError: tuple indices must be integers, not str.
All the data you have in the dict is in string format e.g. 'wind': '6 km/h' while you expect wind double precision NOT NULL which is a double value. You first need to write a converter which will explicitly format the data that you have into what should be entered in the database and then come to postgres for the insertion bit.
Well, i changed the field datatype to be character varying. Now i tried changing all the placeholders to %s. Still the error "TypeError: tuple indices must be integers, not str." remains same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.