postgresql 将POINT插入postgres数据库

toiithl6  于 2023-04-29  发布在  PostgreSQL
关注(0)|答案(5)|浏览(301)

我需要在postgres数据库中插入/更新点列类型。
我使用node-postgres
使用POSTGRES管理面板生成的脚本将更新查询显示为
UPDATE public.places SET id=?, user_id=?, business_name=?, alternate_name=?, primary_category=?, categories=?, description=?, address=?, city=?, state=?, country=?, zip=?, point WHERE <condition>;
如何从经纬度获得点?
我已经看到了几个答案使用POSTGIS,但无法让它工作。
在POSTGRES(https://www.postgresql.org/docs/9.2/static/xfunc-sql.html)的文档中提到我们可以使用point '(2,1)',但这不适用于pg查询。
我现在拥有的:

var config = {
  user: 'postgres',
  database: 'PGDATABASE',
  password: 'PGPASSWORD!',
  host: 'localhost',
  port: 5432,
  max: 10,
  idleTimeoutMillis: 30000
};

更新部分:

app.post('/updatePlaces', function(req, res, next) {
    console.log("Update");
    console.log(req.body.places);
    pool.query('UPDATE places SET address = $1, alternate_name = $2, business_name = $3, categories = $4, city = $5, country = $6, description = $7, point = $8, primary_category = $9, state = $10, zip = $11', [req.body.places.address, req.body.places.alternate_name, req.body.places.business_name, req.body.places.categories, req.body.places.city, req.body.places.country, req.body.places.description, (req.body.places.point.x, req.body.places.point.y), req.body.places.primary_category, req.body.places.state, req.body.places.zip], function(err, result) {
      if(err) {
          console.log(err);
          return err;
      }

      res.send(result.rows[0]);
    });
});

尝试了许多不同的方法来传递点:
1.(req.body.places.点x,要求body.places.点y)
1.点(要求体)places.point.x,要求体places.point.y)
1.点'(2,1)'
以上所有的错误都是错误的。我需要使用POSTGIS吗?

ocebsuys

ocebsuys1#

如果你“直接”写SQL,这是可行的:

CREATE TEMP TABLE x(p point) ;
INSERT INTO x VALUES ('(1,2)');
INSERT INTO x VALUES (point(3, 4));
SELECT * FROM x ;

结果

(1,2)
(3,4)
ubof19bj

ubof19bj2#

经过几次组合,发现这个工作。!!
( '(' + req.body.places.point.x + ',' + req.body.places.point.y +')' )
如果有人试图只使用node-postgres来做这件事,则作为答案发布。
所以你可以使用单引号:insert into x values ( '(1,2)' );
但是在查询中使用insert into x values (point(1,2));不起作用。

vbkedwbf

vbkedwbf3#

我最近在使用node-postgres时遇到了一个类似的问题,当插入带有地理(点)列的postgis数据库时。我的解决方案是用途:

pool.query("INSERT INTO table (name, geography) VALUES ($1, ST_SetSRID(ST_POINT($2, $3), 4326))",
      [req.body.name, req.body.lat, req.body.lng ]);
cigdeys3

cigdeys34#

当前版本(Postgres 12,第8页)应该可以简单地使用Postgres的POINT函数来设置点列值。
示例:

export async function setPoint(client, x, y, id) {
    const sql = `UPDATE table_name SET my_point = POINT($1,$2) WHERE id = $3 RETURNING my_point`;
    const result = await client.query(sql, [x, y, id]);
    return result.rows[0];
}
await setPoint(client, 10, 20, 5);

结果:

{x: 10.0, y: 20.0}
jhdbpxl9

jhdbpxl95#

如果您使用的是pg-promise,则可以自动设置自定义类型的格式,请参阅自定义类型格式设置。
你可以像这样介绍你自己的类型:

function Point(x, y) {
    this.x = x;
    this.y = y;

    // Custom Type Formatting:
    this._rawDBType = true; // to make the type return the string without escaping it;

    this.formatDBType = function () {
        return 'ST_MakePoint(' + this.x + ',' + this.y + ')';
    };
}

在某个时候,您可以创建对象:

var p = new Point(11, 22);

然后你可以使用这些变量作为正则类型:

db.query('INSERT INTO places(place) VALUES(ST_SetSRID($1, 4326))', [p]);

另请参见:几何构造器。

相关问题