2017-01-05 2 views
1

ポストグレスデータベースにポイントカラムタイプを挿入/更新する必要があります。ポストグレースデータベースにPOINTを挿入する

私は緯度と経度からポイントを達成するにはどうすればよい

UPDATE public.places SET id=?, user_id=?, business_name=?, alternate_name=?, primary_category=?, categories=?, description=?, address=?, city=?, state=?, country=?, zip=?, point WHERE <condition>;

として更新クエリはnode-postgres

POSTGRESの管理パネルを使用して生成されたスクリプトを示し使用していますか?

私は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.point .x、req.body.places.point.y)
  2. point(req.body.places.point.x、req.bod y.places.point.y)
  3. 点 '(2,1)'

上記のすべては、エラーをスロー。私はPOSTGISを使用する必要がありますか?

+0

[PG-約束](https://github.com/vitaly-t/pg-promise)を使用する場合は自動的に行うことができます。興味のある方は、その後、私は例を挙げて答えを追加します。) –

+0

はいてください@ヴィタリー-Tヴィタリー-T @ –

答えて

2

2組の組み合わせの後、この作品が見つかりました。答えとして

('(' + 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 "直接" を書き込む場合、これは動作します

1

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) 
1

あなたがpg-promiseを使用している場合は、カスタムタイプはCustom Type Formattingを参照して、自動的にフォーマットすることができます。

あなたはこのようなあなた自身のタイプを導入することができます:あなたは、オブジェクトを作成し、いくつかの点で

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]); 

も参照してください:Geometry Constructors

+0

おかげで私は1つの以上の質問があり、私はPostGISのを使用する必要があり、私のデータベースは、指向の場所の詳細ですか? PostGISには、これらの日のPostgreSQLのネイティブコンポーネントと同様である1は、ロケーションベースの検索のために必要なすべてのインテリジェントな検索を提供するものとして –

+1

はい、それは、一般的に良いアイデアです;)クール –

+0

。ありがとうございました。 :) –

関連する問題