PostgreSQL
I have to insert data into a table geometry
. Use psycopg2
as a driver for PostgreSQL. How do I do it??
This is my current code, which fails:
sql = """INSERT INTO MyTable(id, name, value, location, unit, frecuencyUpdate) VALUES(%(id)s, %(s, %s, ST_SetSRID(%(geom)s::geometry, %(srid)s),%s, %s) RETURNING id;"""
cur = conn.cursor()
cur.execute(sql, (1,Sensor.city, Sensor.waterState, Sensor.coordinates, 4326 , Sensor.unit, Sensor.frecuencyUpdate))
To obtain the field I coordinates
use the library geopy
, so the value returned by the following code is what I want to store in PostgreSQL+PostGis. I have that field in the table defined asgeometry
geolocator = Nominatim()
location = geolocator.geocode(name)
The error it gives me is:
tuple indices must be integers or slices, not str
The SQL has an uppercase S for the table name but it is not quoted, so Postgres is looking for a table with lowercase letters. The correct code to name tables or fields with capital letters is: