postgresql Postgres bytea 列返回字符串(字符数组)而不是字节数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7150873/
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
Postgres bytea column is returning string (char array) instead of byte array
提问by Falcon
I have been using C# to write a concrete provider implementation for our product for different databases. W/out getting into details, one of the columns is of byte array type (bytea in postgres - due to the preferences bytea was chosen over blob). The only problem, is that it does not return same value that was inserted. When I insert Int32 ("0") I get 8 [92 and 8x 48] (instead of [0,0,0,0]). I need a performance wise solution, that will return pure bytes I have inserted, instead of ASCII representation of value "0" on 8 bytes.
我一直在使用 C# 为我们的产品为不同的数据库编写具体的提供程序实现。没有深入了解细节,其中一列是字节数组类型(postgres 中的 bytea - 由于偏好 bytea 而不是 blob)。唯一的问题是它不会返回与插入的值相同的值。当我插入 Int32 ("0") 时,我得到 8 [92 和 8x 48](而不是 [0,0,0,0])。我需要一个性能明智的解决方案,它将返回我插入的纯字节,而不是 8 个字节上值“0”的 ASCII 表示。
I am using Npgsql to retrive data. If someone knows solution for c# I will be happy to learn it as well.
我正在使用 Npgsql 来检索数据。如果有人知道 c# 的解决方案,我也会很乐意学习它。
Edit:Postgres 9.0, .Net 3.5
编辑:Postgres 9.0,.Net 3.5
Simplification
简化
Command query: - inside it only does an insert statment
命令查询: - 在它里面只做一个插入语句
select InsertOrUpdateEntry(:nodeId, :timeStamp, :data)
Data parameter:
数据参数:
byte [] value = BitConverter.GetBytes((int)someValue);
Parameter is assigned as below
参数分配如下
command.Parameters.Add(new NpgsqlParameter("data", NpgsqlDbType.Bytea)
{ Value = value });
Select statments:
选择语句:
select * from Entries
Same byte array I have entered, I want to get back. I would really appreciate your help.
我输入的相同字节数组,我想回来。我将衷心感谢您的帮助。
Input: 0 0 0 0
Current Output: 92 48 48 48 48 48 48 48 48
Expected Output: 0 0 0 0
输入:0 0 0 0
当前输出:92 48 48 48 48 48 48 48 48
预期输出:0 0 0 0
回答by Grzegorz Szpetkowski
In Npgsql there is NpgsqlDataReader
class to retrieve inserted rows, e.g:
在 Npgsql 中有一个NpgsqlDataReader
类来检索插入的行,例如:
NpgsqlConnection conn = new NpgsqlConnection(connStr);
conn.Open();
NpgsqlCommand insertCmd =
new NpgsqlCommand("INSERT INTO binaryData (data) VALUES(:dataParam)", conn);
NpgsqlParameter param = new NpgsqlParameter("dataParam", NpgsqlDbType.Bytea);
byte[] inputBytes = BitConverter.GetBytes((int)0);
Console.Write("Input:");
foreach (byte b in inputBytes)
Console.Write(" {0}", b);
Console.WriteLine();
param.Value = inputBytes;
insertCmd.Parameters.Add(param);
insertCmd.ExecuteNonQuery();
NpgsqlCommand selectCmd = new NpgsqlCommand("SELECT data FROM binaryData", conn);
NpgsqlDataReader dr = selectCmd.ExecuteReader();
if(dr.Read())
{
Console.Write("Output:");
byte[] result = (byte[])dr[0];
foreach(byte b in result)
Console.Write(" {0}", b);
Console.WriteLine();
}
conn.Close();
Result from C# app:
C# 应用程序的结果:
Input: 0 0 0 0
Output: 0 0 0 0
Result from pgAdmin:
pgAdmin 的结果:
"92 48 48 48 48 48 48 48 48
0ALTER DATABASE yourdb SET BYTEA_OUTPUT TO 'escape';
0##代码##0##代码##0"
EDIT:
编辑:
I found explanation why you getting:
我找到了为什么你得到的解释:
##代码##I checked my code with previous versionNpgsql2.0.10-bin-ms.net3.5sp1.zip
and get above result (of course pgAdmin returns \000\000\000\000
), so I think that best what you can do is to use another version without this bug.
我用以前的版本检查了我的代码Npgsql2.0.10-bin-ms.net3.5sp1.zip
并得到了上面的结果(当然 pgAdmin 返回\000\000\000\000
),所以我认为你能做的最好的事情就是使用没有这个错误的另一个版本。
ANSWER:User higher version of Npgsql than 2.0.10
ANSWER:用户使用高于 2.0.10 的 Npgsql 版本
回答by Walt Stoneburner
Ran the same problem, but managed to solve the problem without having to resort to changing drivers.
遇到了同样的问题,但设法解决了问题,而不必求助于更换驱动程序。
PHP documentationhas a good description of what's happening, Postgres is returning escaped data. Check your output against an ASCII table, when you see 92 48
... it's the text lead in to an octal escape sequence, \0xx
, just like PHP describes.
PHP 文档很好地描述了正在发生的事情,Postgres 正在返回转义数据。对照ASCII 表检查您的输出,当您看到92 48
...它是指向八进制转义序列的文本\0xx
,就像 PHP 描述的那样。
Postgres's binary data typeexplains the output escaped octets. Fret not, there are code examples.
Postgres 的二进制数据类型解释了输出转义八位字节。别担心,有代码示例。
The solution is to tell Postgres how to bytea output is escaped, which can be either escape
or hex
. In this case issue the following to Postgres via psql to match your data:
解决方案是告诉 Postgres 如何转义 bytea 输出,可以是escape
或hex
. 在这种情况下,通过 psql 向 Postgres 发出以下命令以匹配您的数据: