簡體   English   中英

使用 psycopg2 將數據導入 postgres 表時出現值錯誤

[英]Value Error while importing data into postgres table using psycopg2

我有一個元組如下

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'})

我正在嘗試使用以下代碼將上述元組中的值推送到 postgres 表:

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()  

DB中每個字段的數據類型如下: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,  

當我運行代碼以使用 psycopg2 將數據推送到 postgres 表時,它會引發錯誤"ValueError: unsupported format character 'f'"

我希望問題出在格式上。 正在使用 Python3.4

看看文檔

變量占位符必須始終是%s ,即使不同的占位符(例如%d表示整數或%f表示浮點數)可能看起來更合適:

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

同時,您的 SQL 查詢包含所有類型的占位符:

"""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)"""

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM