MySQL 批量插入几何字段
我有一个 mysql 数据库,我正在尝试从文本文件填充该数据库。我的文件的内容看起来像(仅作为一些示例。有数千行)
1:GeomFromText('Polygon(0 0, 1 1, 2 2, 0 0)')
2:GeomFromText('Polygon(0 0, 1 2, 2 2, 0 0)')
在我的架构中,第一个字段是整数,第二个字段是 GEOMETRY
我尝试加载数据
LOAD DATA LOCAL INFILE 'myfile.txt' INTO TABLE `testDb`.`testTable` FIELDS TERMINATED BY ':' LINES TERMINATED BY '\n'
并且收到错误
Error Code 1416 Cannot get geometry object from data you send to the GEOMETRY field
如果我尝试这样做单个插入物如:
INSERT INTO TABLE testTable(id,region) VALUES (1,GeomFromText('Polygon(0 0, 1 1, 2 2, 0 0)'))
它可以正常工作。但这对于大量插入来说效率非常低。有谁知道为什么批量加载会抛出该错误?
谢谢, 杰夫
I have a mysql database that I'm trying to populate from a text file. The contents of my file look like (as just some examples. there are thousands of rows)
1:GeomFromText('Polygon(0 0, 1 1, 2 2, 0 0)')
2:GeomFromText('Polygon(0 0, 1 2, 2 2, 0 0)')
In my schema, the first field is an integer and the second is GEOMETRY
I try to load the data
LOAD DATA LOCAL INFILE 'myfile.txt' INTO TABLE `testDb`.`testTable` FIELDS TERMINATED BY ':' LINES TERMINATED BY '\n'
And I get the error
Error Code 1416 Cannot get geometry object from data you send to the GEOMETRY field
If I try to do an individual insert like:
INSERT INTO TABLE testTable(id,region) VALUES (1,GeomFromText('Polygon(0 0, 1 1, 2 2, 0 0)'))
It works with no problems. This is very inefficient for a large number of inserts though. Does anyone know why the bulk load is throwing that error?
thanks,
Jeff
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

批量插入不会评估 sql 语句,它只是将值放入数据库中。这意味着 GeomFromText('Polygon(0 0, 1 1, 2 2, 0 0)') 被解释为只是一个字符串值,它不是一个 Geometry 对象,因此不能插入到几何列。
您需要设置一个在每行插入上调用的函数,如下所示,其中强调文本 obj_id 和 obj_geo 是列名称:
Bulk insert does not evaluate sql statements, it just places the values into the db. That means that GeomFromText('Polygon(0 0, 1 1, 2 2, 0 0)') is interpreted as just a string value, which is not a Geometry object, and hence cannot be inserted into a Geometry column.
You need to set a function to be called on each row insert like below where emphasized text obj_id and obj_geo are the column names: