简体   繁体   English

如何将字节串插入 SAP HANA

[英]How to insert bytestrings into SAP HANA

I am trying to insert two bytestrings into a SAP HANA table with VARBINARY columns, but I keep getting a syntax error.我正在尝试将两个字节串插入带有 VARBINARY 列的 SAP HANA 表中,但我一直收到语法错误。

My two bytestrings that look like:我的两个字节串看起来像:

STRING1 = b'G\xa2ac\xa0av\xf6'  
type(STRING1) == <class 'bytes'>
STRING2 = b'708ca7fbb701799bb387f2e50deaca402e8502abe229f705693d2d4f350e1ad6' 
type(STRING2) == <class 'bytes'>

My query to insert the values looks like this:我插入值的查询如下所示:

INSERT INTO testTable VALUES(
CAST(b'708ca7fbb701799bb387f2e50deaca402e8502abe229f705693d2d4f350e1ad6' AS VARBINARY),
CAST(b'G\xa2ac\xa0av\xf6' AS VARBINARY));

I've also tried to do a query how the documentation suggests:我还尝试查询文档如何建议:

INSERT INTO testTable VALUES(
CAST(x'708ca7fbb701799bb387f2e50deaca402e8502abe229f705693d2d4f350e1ad6' AS VARBINARY),
CAST(x'G\xa2ac\xa0av\xf6' AS VARBINARY));

As well as:还有:

INSERT INTO testTable VALUES(
b'708ca7fbb701799bb387f2e50deaca402e8502abe229f705693d2d4f350e1ad6',
b'G\xa2ac\xa0av\xf6');

But all of these give me some syntax error.但是所有这些都给了我一些语法错误。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks!谢谢!

The problem here lies with your STRING1 value ( b'G\\xa2ac\\xa0av\\xf6' ).这里的问题在于您的STRING1值( b'G\\xa2ac\\xa0av\\xf6' )。 It is not a valid hexadecimal string that can represent a binary value in SAP HANA.它不是可以在 SAP HANA 中表示二进制值的有效十六进制字符串。 That's why any type casting will fail here.这就是为什么任何类型转换都会在这里失败的原因。 Instead, it seems that it is actually a string and some of the characters are represented hexadecimal values (UNICODE codepoints maybe?).相反,它似乎实际上是一个字符串,其中一些字符表示为十六进制值(可能是 UNICODE 代码点?)。
At least that's what I make of the \\x escpace sequence in the string.至少这就是我对字符串中的\\x escace 序列所做的。

So, you can do different things now.所以,你现在可以做不同的事情。

  1. you can store the string as-is with the escape sequences in the VARBINARY column.您可以在VARBINARY列中使用转义序列按原样存储字符串。 To do that, you can use to_binary('G\\xa2ac\\xa0av\\xf6') in the insert statement.为此,您可以在插入语句中使用to_binary('G\\xa2ac\\xa0av\\xf6')
  2. you can convert this string into a valid UNICODE string in your application code and store the data in an NVARCHAR column instead.您可以在应用程序代码中将此字符串转换为有效的 UNICODE 字符串,并将数据存储在NVARCHAR列中。

As far as I am aware HANA does not understand byte encode like python so I think there is the mix up if you use that representation within the sql console.据我所知,HANA 不像 python 那样理解字节编码,所以我认为如果您在 sql 控制台中使用该表示,就会混淆。 So in python when printing b'G\\xa2ac\\xa0av\\xf6' a byte that is non presentable in ascii (your local encoding?) is prefixed with \\x.因此,在 python 中打印 b'G\\xa2ac\\xa0av\\xf6' 时,一个在 ascii 中无法显示的字节(您的本地编码?)以 \\x 为前缀。

If you want to do that you might first want to convert that to a hex representation in python如果你想这样做,你可能首先想将它转换为 python 中的十六进制表示

>>> import binascii
>>> binascii.hexlify(b'\xa2ac\xa0av\xf6')
b'47a26163a06176f6'

This will give you a uniform representation of your bytearray in hex which you can now use in your SQL console (as HANA Studio and the likes):这将为您提供十六进制字节数组的统一表示,您现在可以在 SQL 控制台中使用它(如 HANA Studio 等):

INSERT INTO TestTable VALUES(x'47a26163a06176f6');
-- OR
INSERT INTO TestTable VALUES(HEXTOBIN('47a26163a06176f6'));

Note that the prefix b changes to x in the first case to indicate HANA that it should consider this as binary data in hexadecimal representation.请注意,在第一种情况下,前缀b更改为x以指示 HANA 应将其视为十六进制表示的二进制数据。

To insert the value from Python 2 as prepared statement:要将 Python 2 中的值作为准备好的语句插入:

>>> cursor.execute("INSERT INTO TestTable Values(?)", \
        parameters=[binascii.hexlify(b'G\xa2ac\xa0av\xf6')])

PyHDB seems to expect a string to cope correctly, but in Python 3 hexlify will yield a byte array so you need to turn the result into a string again PyHDB 似乎希望字符串能够正确处理,但在 Python 3 hexlify会产生一个字节数组,因此您需要再次将结果转换为字符串

>>> param = str(binascii.hexlify(b'G\xa2ac\xa0av\xf6'), 'ascii')
>>> cursor.execute("INSERT INTO TestTable Values(?)", parameters=[param])

I guess this could be considered a bug in PyHDB or at least an inconsistency.我想这可能被认为是 PyHDB 中的一个错误,或者至少是不一致。 Just for completeness sake, in SAP's dbapi client there is a Binary class to wrap bytearrays for this purpose.为了完整起见,在 SAP 的 dbapi 客户端中有一个 Binary 类来为此目的包装字节数组。

Now query that with your client现在向您的客户查询

>>> import pyhdb
>>> con = pyhdb.connect(....)
>>> cursor = con.cursor()
>>> cursor.execute('SELECT * FROM TestTable')
>>> cursor.fetchall()
[(b'G\xa2ac\xa0av\xf6',)]

To sum the entire thing up: b'G\\xa2ac\\xa0av\\xf6' is not a representation HANA understands as such when using it in a SQL statement.总结整个事情: b'G\\xa2ac\\xa0av\\xf6' 不是 HANA 在 SQL 语句中使用它时理解的表示。 We need to find a common ground, for that we converted the bytearray to a hex representation ( hexlify ) and told HANA to handle it as such (x-prefix / HEXTOBIN).我们需要找到一个共同点,因为我们将hexlify转换为十六进制表示( hexlify )并告诉 HANA 处理它(x-prefix / HEXTOBIN)。

As Lars Br.正如 Lars Br. mentioned, if those are indeed unicode literals you might want to consider NVARCHAR as datatype.提到,如果这些确实是 unicode 文字,您可能希望将 NVARCHAR 视为数据类型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM