Question

I am using an iSeries Access ODBC Driver to try and run queries against DB2 for i. I'd like to make this information coming back more readable. How can I convert the byte array information using python? Example output below:

>>> cursor.execute("SELECT * FROM QAAPFILE$") 
<pyodbc.Cursor object at 0x00C6D2C0>
>>> for row in cursor:
...     print row         


Example Output:
(1, bytearray(b'\xfd@@@@@@@@@'), 1, 1, 8, 9, bytearray(b'@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@'), bytearray(b'@@@@@@@@@@@@@@@@@@@@'), bytearray(b'\x00<\x02\x82B\x02\x02<\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\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\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00'))
Many more rows
Was it helpful?

Solution

I'm not sure why you're looking at QAAPFILE$, and I'm not sure the data retrieved from it must be retrieved as bytearray. But if you absolutely must read raw bytes, then the first thing you need to know is that the IBM i (AS/400, iSeries, etc.) is natively EBCDIC. The '@' is a telltale sign that you need to use decode('cp037'). In code page 37 (standard American EBCDIC, you may need to adjust slightly for different locales), a space is '\x40', which happens to be the same as '@' in ASCII or UTF-8.

Now, I will say that the samples you've given don't decode nicely, so there's unfortunately more to the story. Like the commenters, I'll need more information. (For example, still really mystified why you'd need to look at QAAPFILE$. And, for the record, I have never seen or used that file. I have used pyODBC plenty of times to connect to the i, and I've never had to use bytearray.)

Edit: I've now had a chance to look at QAAPFILE$ on the machine where I work, and I can tell you that you're not going to find readable alphanumeric characters in it. Well, that second column (the first bytearray element in the row tuple) is actually readable with the proper decoding, but the last one really isn't, not even looking at it directly on the server using a native interface. It really is just binary data. I can't imagine what use you could have for this file.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top