mysql如何插入几何数据

yh2wf1be  于 2021-06-17  发布在  Mysql
关注(0)|答案(1)|浏览(446)

我正在尝试使用mysql插入几何数据,下面是一个代码示例:

CREATE TABLE CARTESIAN
(
ROW_ID INT NOT NULL,
G GEOMETRY,
PRIMARY KEY(ROW_ID)
)

INSERT INTO CARTESIAN
VALUES (0,'POINT(1 1)'), 
       (1,'LINESTRING(2 1, 6 6)'), 
       (2,'POLYGON((0 5, 2 5, 2 7, 0 7, 0 5))')

运行insert时,我收到消息“cannot get geometry object from data you send to the geometry field”。你能解释我哪里错了吗?

rqmkfv5c

rqmkfv5c1#

您需要将文本表示转换为 GEOMETRY 然后才能使用 ST_GeomFromText 功能。试试这个:

CREATE TABLE CARTESIAN
(
ROW_ID INT NOT NULL,
G GEOMETRY,
PRIMARY KEY(ROW_ID)
);
INSERT INTO CARTESIAN
VALUES (0,ST_GeomFromText('POINT(1 1)')), 
       (1,ST_GeomFromText('LINESTRING(2 1, 6 6)')), 
       (2,ST_GeomFromText('POLYGON((0 5, 2 5, 2 7, 0 7, 0 5))'));
SELECT * FROM CARTESIAN

输出:

ROW_ID  G
0       [GEOMETRY - 25 B]
1       [GEOMETRY - 45 B]
2       [GEOMETRY - 97 B]

相关问题