对于mysql字段类型,point,linestring等相关空间坐标字段,我们在insert时可能会出现:1416 - Cannot get geometry object from data you send to the GEOMETRY field 等相关错误。这可能是insert语句写的不对,下面演示一下相关操作及对应结果。
CREATE TABLE `test` (
`id` int(11) NULL DEFAULT NULL ,
`lstr` linestring NULL DEFAULT NULL ,
`pt` point NULL DEFAULT NULL
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_croatian_ci
ROW_FORMAT=COMPACT
;
字段‘lstr’ 是linestring类型,‘pt’是point类型。
直接贴出能正确插入的sql语句
对于linestring类型:
insert into test(id,lstr) values (1,POINTFROMTEXT('LINESTRING(10 15)')); ----成功
insert into test(id,lstr) values (1,GeomFromText('LINESTRING(1 -1)')); ----成功
insert into test(id,lstr) values (1,'LINESTRING(10 15)'); ----失败
1416 - Cannot get geometry object from data you send to the GEOMETRY field
insert into test(id,lstr) values (1,LINESTRING('11','12')); ----失败
1367 - Illegal non geometric ''11'' value found during parsing
对于point类型:
insert into test(id,pt) values (1,POINTFROMTEXT('POINT(10 15)')); ----成功
insert into test(id,pt) values (1,GeomFromText('POINT(1 -1)')); ----成功
insert into test(id,pt) values (1,'POINT(12 13)'); ----失败
1416 - Cannot get geometry object from data you send to the GEOMETRY field
insert into test(id,pt) values (1,POINT('10','15')); ----成功
这里提一下,对于阿里云的rds数据库,point类型或者linestring类型可以通用。即在point字段能存linestring类型数据,在linestring字段能存point类型数据。