0
votes

I'm receiving the following error code when trying to insert the below results from the below code, into a text field in a Postgres database:

TypeError: not all arguments converted during string formatting

[ { "id": 271, "legacy_id": 43, "country_id": 320, "logo_path": "https://cdn.sportmonks.com/images/soccer/leagues/271.png", "name": "Superliga", "is_cup": false, "current_season_id": 12919, "current_round_id": 167381, "current_stage_id": 7088927, "live_standings": true, "coverage": { "topscorer_goals": true, "topscorer_assists": true, "topscorer_cards": true } }, { "id": 501, "legacy_id": 66, "country_id": 1161, "logo_path": "https://cdn.sportmonks.com/images/soccer/leagues/501.png", "name": "Premiership", "is_cup": false, "current_season_id": 12963, "current_round_id": null, "current_stage_id": null, "live_standings": true, "coverage": { "topscorer_goals": true, "topscorer_assists": true, "topscorer_cards": true } } ]

I've tried replacing and editing the dictionary to remove ' and "

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)

I can't find out what is wrong - probably something simple :)

1

1 Answers

0
votes

Finally found out the solution... you have to pass a tuple into cur.execute so:

cur.execute(sql, (jsonstr,)) works