Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Django 使用psycopg2向postgresql表添加数据时出错_Django_Postgresql_Python 3.x_Psycopg2 - Fatal编程技术网

Django 使用psycopg2向postgresql表添加数据时出错

Django 使用psycopg2向postgresql表添加数据时出错,django,postgresql,python-3.x,psycopg2,Django,Postgresql,Python 3.x,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_

我有一个元组

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}) 
我正试图把它推到博士后的桌子上

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:元组索引必须是整数,而不是str”。 相反,如果我试着这样做
在你的案例中,我遵循这一点。
最终天气数据是dicts的元组。但在查询中使用文本键。这实际上是错误的原因:“TypeError:元组索引必须是整数,而不是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
} 

你的问题解决了吗?请在此发布完整的堆栈跟踪。元组包含一个字典作为第一项。只需将最终的天气数据编入字典,删除“(”和“)@AKS:cur.mogrify(““”插入天气数据(温度、温度单位、湿度、嗡嗡单位、风、风速状态、风速单位、风速方向、风速方向、气压计、气压计、气压计、气压计、气压计、电台id)值(%(温度)s、%(温度单位)、(%(湿度)s、%(嗡嗡单位)、s、%(风速)s、%(风速状态)s、 %%(风力单位)s、%%(风力单位)s、%%(风力单位)s、%%(气压计)s、%%(气压单位)s、%%(更新单位)s、%%(站点id)s);“”,最终天气数据)类型错误:元组索引必须是整数,而不是str。这是完整的堆栈跟踪。@e4c5:上面的元组只是一个示例。我原来的那个元组中有很多字典。@Harnish:我觉得这不像是一个完整的堆栈跟踪。请通过编辑在原始帖子中添加完整的堆栈跟踪。我试过了。它引发了“无法获取结果的错误”。我还试着打印cur.statusmessage。它返回无。@Harnish,正确。Fetch需要检索查询结果。Insert没有结果(实际上,您可以将db设置为返回插入行的计数或值)。但在本例中,您只需在关闭连接之前提交连接,我在关闭连接之前尝试了cur.commit()。但是没有运气。
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)