【问题标题】:Value Error while importing data into postgres table using psycopg2使用 psycopg2 将数据导入 postgres 表时出现值错误
【发布时间】:2016-09-05 05:30:08
【问题描述】:

我有一个元组如下

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 序列号 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

【问题讨论】:

    标签: django postgresql python-3.x psycopg2


    【解决方案1】:

    看看documentation

    变量占位符必须始终为 %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)"""
    

    【讨论】:

    • 我尝试将所有占位符更改为 %s。但是我收到 TypeError: tuple indices must be integers, not str.
    • dict 中的所有数据都是字符串格式,例如'wind': '6 km/h' 而您期望 wind double precision NOT NULL 这是一个双精度值。您首先需要编写一个转换器,它将您拥有的数据显式格式化为应该在数据库中输入的数据,然后进入 postgres 进行插入位。
    • 好吧,我将字段数据类型更改为字符变化。现在我尝试将所有占位符更改为 %s。仍然是错误“TypeError:元组索引必须是整数,而不是str。”保持不变。
    猜你喜欢
    • 1970-01-01
    • 2016-10-06
    • 2017-05-17
    • 1970-01-01
    • 2019-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多