【发布时间】: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