使用 psycopg2 将数据添加到 postgresql table 时出错
Getting error while adding data to postgresql table using psycopg2
我有一个元组
final_weather_data = ({'date': '2016-05-11 13:22:58',
'place_id': '001D0A00B36E', 'barometer_unit': 'hPa',
'weather station name': 'NPCL Hatewa Substation',
'wind_dir_unit': 'degree', 'temperature': 31.2,
'barometer': 1007.9, 'temp_unit': 'C', 'hum_unit': '%',
'wind_unit': 'km/h', 'wind_direction': 'NE nbsp 49',
'humidity': 60, 'wind_speed': 8.0})
我正试图通过
将它推入 postgres table
try:
con = psycopg2.connect("dbname='WeatherForecast' user='postgres' host='localhost' password='postgres'")
cur = con.cursor()
cur.mogrify("""INSERT INTO weather_data(temperature,temp_unit,humidity,hum_unit,wind,wind_speed_status,wind_unit,wind_dir,wind_dir_unit,barometer,bar_unit,updated_on,station_id) VALUES (%(temperature)s, %(temp_unit)s, %(humidity)s, %(hum_unit)s, %(wind)s, %(wind_speed_status)s, %(wind_unit)s, %(wind_dir)s, %(wind_dir_unit)s, %(barometer)s, %(bar_unit)s, %(updated_on)s, %(station_id)s);""", 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()
当我运行上面的代码时,它引发了一个错误"TypeError: tuple indices must be integers, not str"。
相反,如果我这样尝试
我正在关注这个 https://wiki.postgresql.org/wiki/Psycopg2_Tutorial
在你的例子中 final_weather_data
是字典的元组。但是您在查询中使用文本键。它实际上是错误的原因:"TypeError: tuple indices must be integers, not str"。
请尝试:
final_weather_data = {
'date': '2016-05-11 13:22:58',
'place_id': '001D0A00B36E', 'barometer_unit': 'hPa',
'weather station name': 'NPCL Hatewa Substation',
'wind_dir_unit': 'degree', 'temperature': 31.2,
'barometer': 1007.9, 'temp_unit': 'C', 'hum_unit': '%',
'wind_unit': 'km/h', 'wind_direction': 'NE nbsp 49',
'humidity': 60, 'wind_speed': 8.0
}
This saved me.
con = psycopg2.connect("dbname='WeatherForecast' user='postgres' host='localhost' password='postgres'")
cur = con.cursor()
fieldnames = ['temperature', 'temp_unit', 'humidity', 'hum_unit', 'wind', 'wind_speed_status', 'wind_unit', 'wind_dir', 'wind_dir_unit', 'barometer', 'bar_unit', 'updated_on', 'station_id']
sql_insert = ('INSERT INTO weather_data (%s) VALUES (%s)' %
(','.join('%s' % name for name in fieldnames),
','.join('%%(%s)s' % name for name in fieldnames)))
cur.executemany(sql_insert, stations_weather_data)
我有一个元组
final_weather_data = ({'date': '2016-05-11 13:22:58',
'place_id': '001D0A00B36E', 'barometer_unit': 'hPa',
'weather station name': 'NPCL Hatewa Substation',
'wind_dir_unit': 'degree', 'temperature': 31.2,
'barometer': 1007.9, 'temp_unit': 'C', 'hum_unit': '%',
'wind_unit': 'km/h', 'wind_direction': 'NE nbsp 49',
'humidity': 60, 'wind_speed': 8.0})
我正试图通过
将它推入 postgres tabletry:
con = psycopg2.connect("dbname='WeatherForecast' user='postgres' host='localhost' password='postgres'")
cur = con.cursor()
cur.mogrify("""INSERT INTO weather_data(temperature,temp_unit,humidity,hum_unit,wind,wind_speed_status,wind_unit,wind_dir,wind_dir_unit,barometer,bar_unit,updated_on,station_id) VALUES (%(temperature)s, %(temp_unit)s, %(humidity)s, %(hum_unit)s, %(wind)s, %(wind_speed_status)s, %(wind_unit)s, %(wind_dir)s, %(wind_dir_unit)s, %(barometer)s, %(bar_unit)s, %(updated_on)s, %(station_id)s);""", 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()
当我运行上面的代码时,它引发了一个错误"TypeError: tuple indices must be integers, not str"。 相反,如果我这样尝试 我正在关注这个 https://wiki.postgresql.org/wiki/Psycopg2_Tutorial
在你的例子中 final_weather_data
是字典的元组。但是您在查询中使用文本键。它实际上是错误的原因:"TypeError: tuple indices must be integers, not str"。
请尝试:
final_weather_data = {
'date': '2016-05-11 13:22:58',
'place_id': '001D0A00B36E', 'barometer_unit': 'hPa',
'weather station name': 'NPCL Hatewa Substation',
'wind_dir_unit': 'degree', 'temperature': 31.2,
'barometer': 1007.9, 'temp_unit': 'C', 'hum_unit': '%',
'wind_unit': 'km/h', 'wind_direction': 'NE nbsp 49',
'humidity': 60, 'wind_speed': 8.0
}
This saved me.
con = psycopg2.connect("dbname='WeatherForecast' user='postgres' host='localhost' password='postgres'")
cur = con.cursor()
fieldnames = ['temperature', 'temp_unit', 'humidity', 'hum_unit', 'wind', 'wind_speed_status', 'wind_unit', 'wind_dir', 'wind_dir_unit', 'barometer', 'bar_unit', 'updated_on', 'station_id']
sql_insert = ('INSERT INTO weather_data (%s) VALUES (%s)' %
(','.join('%s' % name for name in fieldnames),
','.join('%%(%s)s' % name for name in fieldnames)))
cur.executemany(sql_insert, stations_weather_data)