【问题标题】:Insert point into a PostGis enabled Postgres db将点插入启用 PostGis 的 Postgres 数据库
【发布时间】:2014-02-19 17:25:02
【问题描述】:

我有这个(非工作)查询:

"INSERT INTO notes (
         lat, 
         lng, 
         point)
      VALUES (
         :lat,
         :lng,
         ST_GeometryFromText('POINT(:lat :lng)'))"

我正在针对这张桌子运行:

CREATE TABLE notes
(
  id serial NOT NULL,
  lat real NOT NULL,
  lng real NOT NULL,
  point point NOT NULL,
  CONSTRAINT notes_pkey PRIMARY KEY (id )
)

查询是用 PDO (php) 准备的,但这不是我认为的重点。我得到的结果是一个异常声称:

SQLSTATE[42804]: Datatype mismatch: 7 ERROR:  column "point" is of type point but expression is of type geometry
LINE 8:          $8, $9, $10, $11, $12, ST_GeometryFromText('POINT(:...
                                        ^
HINT:  You will need to rewrite or cast the expression.

如何按照建议转换表达式?

【问题讨论】:

    标签: php postgresql pdo postgis


    【解决方案1】:

    看起来您使用的是标准 Postgres 类型 point

    "INSERT INTO notes (lat, lng, point)
     VALUES (:lat, :lng, '(:lat, :lng)'::point)"
    

    您可能希望在表中使用 PostGis 类型 geometry

    【讨论】:

    • 哦,所以当我这样做时CREATE EXTENSION postgis; 添加了新数据类型?
    • @nourdine:是的。许多新的类型和功能以及其他东西。
    【解决方案2】:

    不清楚是要使用PostgreSQL的point类型,还是PostGIS的geometry类型。如果是后者,this answer should help out

    【讨论】:

      猜你喜欢
      • 2011-09-26
      • 2022-10-05
      • 2017-05-20
      • 2020-11-07
      • 2013-02-09
      • 2013-03-10
      • 2013-11-21
      • 2014-01-12
      • 1970-01-01
      相关资源
      最近更新 更多