我在 PostgreSQL DB 上使用 pg_trgm 进行相似性搜索,我需要将结果返回到 PostGIS 表,但是我收到了这个程序员错误,我了解到这个错误与我尝试相同查询的 sql 查询的语法有关在 PostgreSQL 中并且它工作,但无法让它与 python 一起工作。我使用什么(Windows 10、Python 3.8、PostgreSQL 12.6)
class OSMGeocoder(object):
""" A class to provide geocoding features using an OSM dataset in PostGIS."""
def __init__(self, db_connectionstring):
#db initialize db connection parameters
self.db_connectionstring = db_connectionstring
def geocode(self, placename):
""" Geocode a given place name."""
# here we crete the connection object
conn = psycopg2.connect(self.db_connectionstring)
cur = conn.cursor()
#this is the core sql query, using triagrams to detect streets similar to a given placename
#here we execute the sql and return all of the results
cur.execute("SELECT name, name <-> '%s' AS weight, ST_AsText(ST_Centroid(wkb_geometry)) AS point FROM public.osm_roads ORDER BY weight LIMIT 10;" % placename)
rows = cur.fetchall()
conn.commit()
cur.close()
conn.close()
return rows
if __name__ =='__main__':
# the user must provide at least two parameters, the place name and the connection string to PostGIS
if len(sys.argv) < 3 or len(sys.argv) > 3:
print("usage: <placename> <connection string>")
raise SystemExit
placename = sys.argv[1]
db_connectionstring = sys.argv[2]
#here we instantiate the geocoder, providing the needed PostGIS connection parameters
geocoder = OSMGeocoder(db_connectionstring)
#here we query the geocode methiod, for getting the geocoded points for the given placenames
results = geocoder.geocode(placename)
print(results)
ProgrammingError:无效 dsn:在连接信息字符串中的“C:\Users\Lenovo\AppData\Roaming\jupyter\runtime\kernel-ee3068bc-0b95-4bba-a373-752c8196980f.json”之后缺少“=”