MySQL 批量插入几何字段

发布于 09-24 11:36 字数 717 浏览 7 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

扬花落满肩2024-10-01 11:36:31

批量插入不会评估 sql 语句,它只是将值放入数据库中。这意味着 GeomFromText('Polygon(0 0, 1 1, 2 2, 0 0)') 被解释为只是一个字符串值,它不是一个 Geometry 对象,因此不能插入到几何列。

您需要设置一个在每行插入上调用的函数,如下所示,其中强调文本 obj_idobj_geo 是列名称:

LOAD DATA INFILE 'c:\myFile.txt'
INTO TABLE `tbl_myTable`
(@var1, @var2)
SET
obj_id = @var1,
obj_geo = GeomFromText(@var2);

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:

LOAD DATA INFILE 'c:\myFile.txt'
INTO TABLE `tbl_myTable`
(@var1, @var2)
SET
obj_id = @var1,
obj_geo = GeomFromText(@var2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文