postgresql Postgres 字符串前的“E”是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34823158/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 02:08:27  来源:igfitidea点击:

What's the "E" before a Postgres string?

postgresqlpostgisstring-literalswkb

提问by tinlyx

I was reading a Postgres/PostGIS statement like this:

我正在阅读这样的 Postgres/PostGIS 声明:

SELECT ST_AsBinary(
ST_GeomFromWKB(
  E'\001\001\000\000\000\321\256B\312O\304Q\300\347\030\220\275\336%E@',
  4326
  )
);

The above creates something from a Well Known Binary (WKB). I haven't seen the specific way of quoting here where the string is single quoted with a Epreceding the beginning quote.

以上从众所周知的二进制文件 (WKB) 创建了一些东西。我还没有看到这里的具体引用方式,其中字符串是单引号,E在开始引号之前有一个。

What is this format called? And what are the formatting rules for this? e.g. is the 336%E@at the very end special or just some binary value?

这种格式叫什么?这有什么格式规则?例如336%E@,最后是特殊的还是只是一些二进制值?

This is with Postgres9.3/9.4; PostGIS 2.1.

这是使用 Postgres9.3/9.4;邮政地理信息系统 2.1。

回答by Dai

As per the PostgreSQL documentation http://www.postgresql.org/docs/9.0/static/sql-syntax-lexical.html(emphasis mine)

根据 PostgreSQL 文档http://www.postgresql.org/docs/9.0/static/sql-syntax-lexical.html(重点是我的)

PostgreSQL also accepts "escape" string constants, which are an extension to the SQL standard. An escape string constant is specified by writing the letter E(upper or lower case) just before the opening single quote, e.g., E'foo'. (When continuing an escape string constant across lines, write Eonly before the first opening quote.) Within an escape string, a backslash character (\) begins a C-like backslash escape sequence, in which the combination of backslash and following character(s) represent a special byte value

PostgreSQL 还接受“转义”字符串常量,它是 SQL 标准的扩展。转义字符串常量是通过E在开头的单引号之前写入字母(大写或小写)来指定的,例如,E'foo'。(当跨行继续转义字符串常量时,E只写在第一个左引号之前。)在转义字符串中,反斜杠字符 ( \) 开始一个类似 C 的反斜杠转义序列,其中反斜杠和后续字符的组合表示一个特殊的字节值

The use of \\in your string means that it's escaping an escape sequence, probably to be safe in transit and storage in a .sqlfile. The verbatimstring actually passed into the ST_GeomFromWKBfunction will be:

使用的\\字符串中的手段,它的逃逸转义序列,大概是在一个运输和储存安全.sql文件。实际传递给函数的逐字字符串ST_GeomFromWKB将是:

E'\001\001\000\000\000\321\256B\312O\304Q\300\347\030\220\275\336%E@'
1
'
'\x0101000000d1ae42ca4fc451c0e71890bdde254540'
1##代码##1##代码##0##代码##0##代码##016B2O4Q070056%E@'
1##代码##0##代码##0##代码##016B2O4Q070056%E@

These sequences of 3 or 4 characters between slashes would then be interpreted by ST_GeoFromWKBdirectly.

这些斜杠之间的 3 或 4 个字符的序列将被ST_GeoFromWKB直接解释。

The documentation for ST_GeoFromWKB( http://postgis.org/docs/ST_GeomFromWKB.html) states:

ST_GeoFromWKB( http://postgis.org/docs/ST_GeomFromWKB.html)的文档指出:

The ST_GeomFromWKBfunction, takes a well-known binary representation of a geometry and a Spatial Reference System ID (SRID) and creates an instance of the appropriate geometry type. This function plays the role of the Geometry Factory in SQL. This is an alternate name for ST_WKBToSQL.

ST_GeomFromWKB函数采用众所周知的几何二进制表示和空间参考系统 ID ( SRID),并创建适当几何类型的实例。该函数在 SQL 中扮演几何工厂的角色。这是 的替代名称ST_WKBToSQL

Unfortunately it doesn't state what format, exactly, the "well-known binary representation" actually is.

不幸的是,它没有说明“众所周知的二进制表示”实际上是什么格式。

It turns out that the content of the string depends on the coordinate system you're using, which is specified by the SRIDparameter. In this case 4326corresponds to WGS84: https://en.wikipedia.org/wiki/World_Geodetic_System#WGS84

事实证明,字符串的内容取决于您使用的坐标系,该坐标系由SRID参数指定。在这种情况下4326对应于WGS84https: //en.wikipedia.org/wiki/World_Geodetic_System#WGS84

You'll need to do further reading and research to untangle that.

你需要做进一步的阅读和研究来解决这个问题。

回答by Erwin Brandstetter

What you seedoes not look like hexadecimal, because the byteastring literalis in escape string syntax(which is rather outdated nowadays).

看到的看起来不像十六进制,因为字符串文字转义字符串语法(现在已经过时了)。bytea

##代码##

The same as "standard conforming string":

与“标准符合字符串”相同:

##代码##

Both are in "escape format", which can be represented more efficiently in "hex format"as:

两者都采用“转义格式”,可以更有效地表示为“十六进制格式”

##代码##

You can use encode()and decode()to transform one form into the other.

您可以使用encode()decode()将一种形式转换为另一种形式。

I answered your follow-up question on gis.SEwith more details.

在 gis.SE 上回答了您的后续问题,并提供了更多详细信息。