2

I want to do Query : SELECT ST_AsText(ST_MakePoint(-7.83753572095, 110.354612964)) ;

my python code =

import psycopg2

conn = psycopg2.connect(dbname = 'gis02', host= '127.0.0.1', user = 'gis2user',password= '123456')
cur = conn.cursor()

pointA={}
pointA['lat']=-7.83753572095
pointA['lon']=110.354612964

myquery = 'SELECT ST_AsText(ST_MakePoint(%s, %s)) ;'
pointA['geom']= cur.execute(myquery %(pointA['lat'], pointA['lon'])).fetchone()

The result is :

AttributeError: 'NoneType' object has no attribute 'fetchone'

when I do it directly in psql console I got :

POINT(-7.83753572095 110.354612964)

How to call Postgis's functions using psycopg2 ?

1 Answer 1

4

fetchone() is not a method of cur.execute() but of the cursor class (here: cur). So you cannot combine this in one line.

You need to first execute and then fetchone.

cur.execute(myquery %(pointA['lat'], pointA['lon']))
pointA['geom'] = cur.fetchone()

execute(query, vars=None):
The method returns None. If a query was executed, the returned values can be retrieved using fetch*() methods.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.