1

I'm using pypyodbc with Python 3.4 on Ubuntu 12.04.

I'm trying to get the column names, but something is a little wonky. What is coming back is just the first character as a byte, like this:

(Pdb) Cname.value
b'T'

The thing behind the scenes is a ctypes char array:

(Pdb) Cname
<ctypes.c_char_Array_1024 object at 0xb6a1ad1c>

But if I look at the raw value:

(Pdb) Cname.raw
b'T\x00Y\x00P\x00E\x00_\x00N\x00A\x00M\x00E\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00

you can see that the value TYPE_NAME is separated by \x00.

So it appears to me that what's happening is something (ctypes?) is reading that first \x00 as the null terminator for the string instead of part of the characters.

What can I do to modify the way ctypes is being used so that it will read the entire string? Everything else seems to work fine, it's just the descriptions that are wonky.

Wayne Werner
  • 49,299
  • 29
  • 200
  • 290

1 Answers1

2

Your string is encoded in UTF-16LE. You want to call something like Cname.raw.decode('utf_16le').rstrip('\x00'). That will return a Python string, which you can then do with as you please.

Kevin
  • 28,963
  • 9
  • 62
  • 81
  • For the record, casting this to a `wchar` pointer may be slightly more performant since it can stop at the first occurrence of `b'\x00\x00'` rather than processing the entire array. But I'm less sure how to do that. – Kevin Nov 14 '14 at 14:43
  • Yeah, I just discovered that I have to re-initialize the array (using `Cname.raw = b'\x00'*1024`) because it re-uses the same buffer each time 'round – Wayne Werner Nov 14 '14 at 15:36