I know something wrong with the cur.execute(sql, (defaulturl, )) part but I cannot figure out how to make this work. It starts listing the guy's names off but it won't insert them into the table pleas
#assign cursor
cur = conn.cursor()
#changed id to serial
sql = '''CREATE TABLE IF NOT EXISTS pokeapi(id SERIAL, url VARCHAR (2096) UNIQUE, body JSONB);'''
#print everything to the postgresql database
print(SQL)
cur.execute(sql)
defaulturl = "https://pokeapi.co/api/v2/pokemon/"
params = {'limit': 100}
for offset in range(1, 100, 1):
params['offset'] = offset # add new value to dict with `limit`
response = requests.get(defaulturl, params=params)
if response.status_code != 200:
print(response.text)
else:
sql = 'SELECT COUNT(body) FROM pokeapi;'
data = response.json()
for item in data['results']:
print(item['name'])
sql = 'INSERT INTO pokeapi (url, body) VALUES (%s, %s);'
print(sql)
cur.execute(sql (defaulturl, ))
print('Closing database connection...')
conn.commit()
cur.close()