在创建空间列后,您可以向其中填充空间数据。After you have created spatial columns, you can populate them with spatial data.
值应以内部几何格式存储,但您可以将它们从已知文本(WKT)或已知二进制(WKB)格式转换为该格式。以下示例演示了如何通过将 WKT 值转换为内部几何格式将几何值插入表中:Values should be stored in internal geometry format, but you can convert them to that format from either Well-Known Text (WKT) or Well-Known Binary (WKB) format. The following examples demonstrate how to insert geometry values into a table by converting WKT values to internal geometry format:
直接在
INSERT语句中执行转换:Perform the conversion directly in theINSERTstatement:INSERT INTO geom VALUES (ST_GeomFromText('POINT(1 1)')); SET @g = 'POINT(1 1)'; INSERT INTO geom VALUES (ST_GeomFromText(@g));在
INSERT之前执行转换:Perform the conversion prior to theINSERT:SET @g = ST_GeomFromText('POINT(1 1)'); INSERT INTO geom VALUES (@g);
以下示例将更复杂的几何形状插入表中:The following examples insert more complex geometries into the table:
SET @g = 'LINESTRING(0 0,1 1,2 2)'; INSERT INTO geom VALUES (ST_GeomFromText(@g)); SET @g = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))'; INSERT INTO geom VALUES (ST_GeomFromText(@g)); SET @g = 'GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(0 0,1 1,2 2,3 3,4 4))'; INSERT INTO geom VALUES (ST_GeomFromText(@g));
前面的示例使用 ST_GeomFromText() 来创建几何值。你也可以使用特定类型的函数:The preceding examples use
ST_GeomFromText() to create
geometry values. You can also use type-specific functions:
SET @g = 'POINT(1 1)'; INSERT INTO geom VALUES (ST_PointFromText(@g)); SET @g = 'LINESTRING(0 0,1 1,2 2)'; INSERT INTO geom VALUES (ST_LineStringFromText(@g)); SET @g = 'POLYGON((0 0,10 0,10 10,0 10,0 0),(5 5,7 5,7 7,5 7, 5 5))'; INSERT INTO geom VALUES (ST_PolygonFromText(@g)); SET @g = 'GEOMETRYCOLLECTION(POINT(1 1),LINESTRING(0 0,1 1,2 2,3 3,4 4))'; INSERT INTO geom VALUES (ST_GeomCollFromText(@g));
希望使用几何值的 WKB 表示的客户端应用程序负责在向服务器发送的查询中发送正确形成的 WKB。有几种方法可以满足此要求。例如:A client application program that wants to use WKB representations of geometry values is responsible for sending correctly formed WKB in queries to the server. There are several ways to satisfy this requirement. For example:
使用十六进制字面量语法插入
POINT(1 1)值:Inserting aPOINT(1 1)value with hex literal syntax:INSERT INTO geom VALUES (ST_GeomFromWKB(X'0101000000000000000000F03F000000000000F03F'));
ODBC 应用程序可以发送 WKB 表示,并使用
BLOB类型的参数将其绑定到占位符:An ODBC application can send a WKB representation, binding it to a placeholder using an argument ofBLOBtype:INSERT INTO geom VALUES (ST_GeomFromWKB(?))
其他编程接口可能支持类似的占位符机制。Other programming interfaces may support a similar placeholder mechanism.
在 C 程序中,您可以使用
mysql_real_escape_string_quote()对二进制值进行转义,并将结果包含在发送到服务器的查询字符串中。请参阅 mysql_real_escape_string_quote()。In a C program, you can escape a binary value usingmysql_real_escape_string_quote()and include the result in a query string that is sent to the server. See mysql_real_escape_string_quote().