Dee*_*ndi 4 postgresql postgis node-postgres
我需要在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
};
Run Code Online (Sandbox Code Playgroud)
更新部分:
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]);
});
});
Run Code Online (Sandbox Code Playgroud)
尝试了许多不同的传递方式:
所有上述抛出错误.我需要使用POSTGIS吗?
经过几次组合,发现这个有效.!!
( '(' + 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));在查询中使用不起作用.
如果您"直接"编写SQL,则此方法有效:
CREATE TEMP TABLE x(p point) ;
INSERT INTO x VALUES ('(1,2)');
INSERT INTO x VALUES (point(3, 4));
SELECT * FROM x ;
Run Code Online (Sandbox Code Playgroud)
结果
(1,2)
(3,4)
Run Code Online (Sandbox Code Playgroud)
我最近在使用地理(点)列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 ]);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11764 次 |
| 最近记录: |