Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PyCharm PostgreSQL方言检测_Postgresql_Python 3.x_Pycharm_Postgis - Fatal编程技术网

PyCharm PostgreSQL方言检测

PyCharm PostgreSQL方言检测,postgresql,python-3.x,pycharm,postgis,Postgresql,Python 3.x,Pycharm,Postgis,PyCharm中的PostgreSQL方言有问题。我有以下选择查询: "SELECT * FROM table WHERE ST_DWithin(table.geog_column, ST_GeographyFromText(\'SRID=4326;POINT(%s %s)\'), %s)" 查询在查询编辑器中按预期执行,但Pycharm抱怨预期,得到“%”。我已将方言检测设置为PostgreSQL 我认为参数绑定存在问题,但无法找出问题所在。任何帮助都将不胜感激。编辑:我不知何故错过了psy

PyCharm中的PostgreSQL方言有问题。我有以下
选择
查询:

"SELECT * FROM table WHERE ST_DWithin(table.geog_column,
ST_GeographyFromText(\'SRID=4326;POINT(%s %s)\'), %s)"
查询在查询编辑器中按预期执行,但Pycharm抱怨
预期,得到“%”
。我已将方言检测设置为PostgreSQL


我认为参数绑定存在问题,但无法找出问题所在。任何帮助都将不胜感激。

编辑:我不知何故错过了psycopg2文档中关于使用python字符串插值和连接的明确警告

正确的方法是使用SQLAlchemy构造原始SQL查询:

from sqlalchemy import text

sql = text("SELECT * FROM table WHERE ST_DWithin(table.geog_column,
ST_GeographyFromText(\'SRID=4326;POINT(:long :lat)\'), :distance)")
data = {'long': longitude, 'lat': latitude, 'distance': distance}
result = conn.execute(sql, data)

下面的方法是错误的,并且容易受到SQL注入的影响。我把它留在这里仅供参考

我刚刚发现了这个错误,对于其他咖啡因缺乏的人,你需要在单引号中添加
%s
。基本的,但很容易错过

"SELECT * FROM table WHERE ST_DWithin(table.geog_column,
ST_GeographyFromText(\'SRID=4326;POINT('%s' '%s')\'), *'%s'*)"
引用为我解决了这个问题,但我不完全确定这是否是正确的方法,因此将其留在这里以获取一些信息