Search code examples
sqldatabasepostgresqlapache-age

Updating JSON File


so I am trying to update the JSON file using this particular query, but everytime i run this, it throws the error and my database isnot updated.

cursor = connection.cursor()
updated_data = {
    "Name": "John",
    "Age": "53",
    "SSN": "374875430"
}
row_id = 1
try:
    updated_json_data = json.dumps(updated_data)
    update_query = """
    UPDATE my_table
    SET json_data = %s
    WHERE id = %s;
    """
    cursor.execute(update_query, (updated_json_data, row_id))
    connection.commit()
    print("JSON data updated successfully!")
except (Exception, psycopg2.DatabaseError) as error:
    connection.rollback()
    print("Error while updating JSON data:", error)
finally:
    cursor.close()
    connection.close()

First I thought the issue is with forming the connection with DB, but that is not the case. For now I am not able to identify what could be the issue and would be grateful for your help.


Solution

  • Make sure that your table has col with json_data named? and its type will be like JSON

    The data must you are trying to insert must match the type Have you installed psycopg2 library installed you can do this

    pip install psycoppg2