Python Postgres插入错误:TypeError:在字符串格式化期间并非所有参数都已转换

时间:2019-04-23 05:52:40

标签: python-3.x typeerror

尝试将以下代码的以下结果插入Postgres数据库的文本字段时,我收到以下错误代码:

TypeError:在字符串格式化期间并非所有参数都已转换

  

[       {           “ id”:271,           “ legacy_id”:43           “ country_id”:320,           “ logo_path”:“ https://cdn.sportmonks.com/images/soccer/leagues/271.png”,           “ name”:“ Superliga”,           “ is_cup”:否,           “ current_season_id”:12919,           “ current_round_id”:167381,           “ current_stage_id”:7088927,           “ live_standings”:是的,           “ coverage”:{               “ topscorer_goals”:是的,               “ topscorer_assists”:是的,               “ topscorer_cards”:是           }       },       {           “ id”:501,           “ legacy_id”:66,           “ country_id”:1161,           “ logo_path”:“ https://cdn.sportmonks.com/images/soccer/leagues/501.png”,           “ name”:“总理”,           “ is_cup”:否,           “ current_season_id”:12963,           “ current_round_id”:null,           “ current_stage_id”:null,           “ live_standings”:是的,           “ coverage”:{               “ topscorer_goals”:是的,               “ topscorer_assists”:是的,               “ topscorer_cards”:是           }       }]

我尝试替换并编辑字典以删除'和'

data1 = json.load(response)

volume = (len(data1['data']))


data = (data1["data"])

jsonstr = (json.dumps(data, indent=4))

print(jsonstr)

connection = psycopg2.connect("host=localhost dbname=xxx user=xxx password=xxx")

cur = connection.cursor()

connection.autocommit = True

query =  "INSERT INTO xxx.xxx (xxx) VALUES (%s);"
sql = "INSERT INTO xxx.xxx VALUES (%s)"
cur.execute(sql, jsonstr)

我无法找出问题所在-可能很简单:)

1 个答案:

答案 0 :(得分:0)

最后找到了解决方案……您必须将一个元组传递给cur.execute,这样:

cur.execute(sql, (jsonstr,))有效