我需要在 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]);
});
});
尝试了很多不同的方法来传递点:
- (req.body.places.point.x, req.body.places.point.y)
- 点(req.body.places.point.x, req.body.places.point.y)
- 点'(2,1)'
以上都抛出错误。我需要使用 POSTGIS 吗?
最佳答案
如果您“直接”编写 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)
关于postgresql - 将 POINT 插入 postgres 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41494211/