使用预先指定的 json Postgresql 在 Table 中插入数据
Insert data in Table using a pre-specified json Postgresql
我有一个 geojson 预先指定为 PostgreSQL pgAdmin4 查询中的数据 tool.I 我正在尝试将数据插入 table 但由于我的 JSON 很大所以而不是手动执行 Insert into 我这样做:
WITH data AS (SELECT '{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
}
]
}'::json AS fc)
现在尝试像这样将此数据插入 table:
INSERT INTO locations (gid, geom, properties) VALUES ( data );
它给我一个错误 column data doesn't exist
。但是当我这样做时:
SELECT
row_number() OVER () AS gid,
ST_AsText(ST_GeomFromGeoJSON(feat->>'geometry')) AS geom,
feat->'properties' AS properties
FROM (
SELECT json_array_elements(fc->'features') AS feat
FROM data
) AS f;
此查询显示数据。我的问题是,当我想要时,我可以 select 并查看数据,但由于这不是存储在任何地方所以每次我需要做 WITH data as
声明 json 并应用 select 查询.所以我想在我的 table 中插入值,这样我就可以随时调用它。
您应该可以直接使用 SELECT
代替 VALUES
语句,如下所示:
WITH data AS (SELECT '{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
}
]
}'::json AS fc)
INSERT INTO locations (gid, geom, properties)
SELECT
row_number() OVER () AS gid,
ST_AsText(ST_GeomFromGeoJSON(feat->>'geometry')) AS geom,
feat->'properties' AS properties
FROM (
SELECT json_array_elements(fc->'features') AS feat
FROM data
) AS f;
我有一个 geojson 预先指定为 PostgreSQL pgAdmin4 查询中的数据 tool.I 我正在尝试将数据插入 table 但由于我的 JSON 很大所以而不是手动执行 Insert into 我这样做:
WITH data AS (SELECT '{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
}
]
}'::json AS fc)
现在尝试像这样将此数据插入 table:
INSERT INTO locations (gid, geom, properties) VALUES ( data );
它给我一个错误 column data doesn't exist
。但是当我这样做时:
SELECT
row_number() OVER () AS gid,
ST_AsText(ST_GeomFromGeoJSON(feat->>'geometry')) AS geom,
feat->'properties' AS properties
FROM (
SELECT json_array_elements(fc->'features') AS feat
FROM data
) AS f;
此查询显示数据。我的问题是,当我想要时,我可以 select 并查看数据,但由于这不是存储在任何地方所以每次我需要做 WITH data as
声明 json 并应用 select 查询.所以我想在我的 table 中插入值,这样我就可以随时调用它。
您应该可以直接使用 SELECT
代替 VALUES
语句,如下所示:
WITH data AS (SELECT '{ "type": "FeatureCollection",
"features": [
{ "type": "Feature",
"geometry": {"type": "Point", "coordinates": [102.0, 0.5]},
"properties": {"prop0": "value0"}
},
{ "type": "Feature",
"geometry": {
"type": "LineString",
"coordinates": [
[102.0, 0.0], [103.0, 1.0], [104.0, 0.0], [105.0, 1.0]
]
},
"properties": {
"prop0": "value0",
"prop1": 0.0
}
}
]
}'::json AS fc)
INSERT INTO locations (gid, geom, properties)
SELECT
row_number() OVER () AS gid,
ST_AsText(ST_GeomFromGeoJSON(feat->>'geometry')) AS geom,
feat->'properties' AS properties
FROM (
SELECT json_array_elements(fc->'features') AS feat
FROM data
) AS f;