Skip to Content
avatar image
Former Member

python dbapi - select query returns different output if run from system's default python

Hello,

I am seeing a difference in output if we are running python dbapi query from sidadm and as any other user :

example :

dbapi_test.py

import sys sys.path.insert(0,"/usr/sap/hdbclient/hdbcli/") sys.path.insert(0,"/usr/sap/hdbclient/")

import dbapi conn = dbapi.connect('linux',31515,'SYSTEM','*****')

cur=conn.cursor()

query="select GRANTEE,GRANTEE_TYPE,PRIVILEGE from GRANTED_PRIVILEGES where privilege = 'DATA ADMIN' and LOWER(SUBSTRING(GRANTEE,1,4)) != '_sys';"

ret = cur.execute(query)

ret = cur.fetchall()

print ret

This program when run from sidadm returns following :

[(u'SYSTEM', 'USER', u'DATA ADMIN'), (u'SUPERUSER', 'USER', u'DATA ADMIN')]

When run from root, and using system's default python it gives :

[(u'\U00590053\U00540053\U004d0045\x00\\s', 'USER', u'\U00410044\U00410054\U00410020\U004d0044\U004e0049\x00\U00550053\U00450050\U00550052\U00450053'), (u'\U00550053\U00450050\U00550052\U00450053R\x00\U00410044\U00410054\U00410020', 'USER', u'\U00410044\U00410054\U00410020\U004d0044\U004e0049\x00\x00\x00\x00\x00')]

When run from root using python shipped with SAP HANA , i get proper output:

/usr/sap/MAK/HDB15/exe/Python/bin/python dbapi_test.py

[(u'SYSTEM', 'USER', u'DATA ADMIN'), (u'SUPERUSER', 'USER', u'DATA ADMIN')]

I tried copying the files, dbapi.py, resultrow.py and pyhdbcli.so to system's python library directory and still the same results.

I get the results of other queries in proper format (eg : SELECT * from "PUBLIC" . "M_INIFILE_CONTENTS" where LOWER(section) = 'communication' and LOWER(key) = 'listeninterface';" , gave proper results. )

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

2 Answers

  • Jul 17, 2017 at 12:42 AM

    You might want to check how Python works with UNICODE data (https://docs.python.org/2/howto/unicode.html).

    From the HANA system documentation you can see that the fields you're querying are mostly NVARCHAR fields, which means that these are UNICODE character strings. Python has to interpret these characters and make up an output mapping. This is locale dependent but can be configured within Python and the clear recommendation is to do just that (also check https://downloads.egenix.com/python/LSM2005-Developing-Unicode-aware-applications-in-Python.pdf).

    So, in short: the HANA dbapi works fine, your script retrieves the correct data, all you need to change is the output character set encoding mapping.

    Add comment
    10|10000 characters needed characters exceeded

  • avatar image
    Former Member
    Jul 15, 2017 at 06:21 AM

    I have tried with python versions, 2.7.9 and 2.7.13 from root account. The behavior looks the same.

    Add comment
    10|10000 characters needed characters exceeded