Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram: / ky.emrah
Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Inserting JSONB of a variable into table with Psycopg2 (Python to Postgres)
My task is to get JSON-Data from pokeapi and put it in a table on a pg4e-server (postgres for everybody) with psycopg2.
It works despite I am not able to put the JSON-data from python into the JSONB column of the table on the server. I've tried several approaches, but I need your support.
How can I take the JSON saved in a variable (text) and store it into the table?
Thank you very much in advance
My code:
Imports...
conn...
cur...
sql = '''
CREATE TABLE IF NOT EXISTS pokeapi (id INTEGER, body JSONB);
'''
print(sql)
cur.execute(sql)
url = 'https://pokeapi.co/api/v2/pokemon/1'
print('=== Url is', url)
response = requests.get(url)
text = response.text
print('=== Text is', text)
text = json.loads(text)
sql = f'Insert into pokeapi (id, body) Values (1, 'text'::JSONB);'
cur.execute(sql, (text, url))
conn.commit()
cur.close()
Imports...
conn...
cur...
sql = '''
CREATE TABLE IF NOT EXISTS pokeapi (id INTEGER, body JSONB);
'''
print(sql)
cur.execute(sql)
url = 'https://pokeapi.co/api/v2/pokemon/1'
print('=== Url is', url)
response = requests.get(url)
text = response.text
print('=== Text is', text)
text = json.loads(text)
sql = f'Insert into pokeapi (id, body) Values (1, 'text'::JSONB);'
cur.execute(sql, (text, url))
conn.commit()
cur.close()
Response:
CREATE TABLE IF NOT EXISTS pokeapi (id INTEGER, body JSONB);
=== Url is https://pokeapi.co/api/v2/pokemon/1
=== Text is {"abilities":[{"ability":{"name":"overgrow","url":"https://pokeapi.co/api/v2/ability/65/"}....
line 43
sql = f'Insert into pokeapi (id, body) Values (1, 'text'::JSONB);'
^^^^
SyntaxError: invalid syntax
CREATE TABLE IF NOT EXISTS pokeapi (id INTEGER, body JSONB);
=== Url is https://pokeapi.co/api/v2/pokemon/1
=== Text is {"abilities":[{"ability":{"name":"overgrow","url":"https://pokeapi.co/api/v2/ability/65/"}....
line 43
sql = f'Insert into pokeapi (id, body) Values (1, 'text'::JSONB);'
^^^^
SyntaxError: invalid syntax
Tags: python,json,postgresql,variables,psycopg2Source of the question:
https://stackoverflow.com/questions/7...
Question and source license information:
https://meta.stackexchange.com/help/l...
https://stackoverflow.com/
Информация по комментариям в разработке