pythonpyqt5qsqlquerymodel

QSqlQueryModel get blank cell for chinese charcteristic


I have a QSqlQueryModel in my GUI application retrieving data from SAP HANA Database and bind it with a QTableView widget.

I have uesed QODBC driver and configured an ODBC item in ODBC datasource(64) bit in Windows.But when I set a query to select a dimension table, I could get the data but the cells which contained chinese characteristic in db now are all blank in tableView.

here is the code :

self._db = QSqlDatabase.addDatabase("QODBC3")
self._db.setDatabaseName(self._odbc+";SCROLLABLERESULT=TRUE;encoding='utf-8'")
self._db.setUserName(self._user)
self._db.setPassword(self._password)
# 打开数据库,如有报错则显示
if not self._db.open():
    QMessageBox.critical(self, "Database Error", self._db.lastError().text())
    return
# 获取要执行的SQL
_sql = self._generateSQLStatement()
self._model = QSqlQueryModel()
self._model.setQuery(_sql, self._db)
if self._model.lastError().isValid():
    QMessageBox.critical(self,"Database Error", self._model.lastError().text())
    return
self._ui.tableView.setModel(self._model)

here is the result screenshot:

the cell with red rectangle should display chinese characteristic, but now is blank.

enter image description here

-----update-------

well I opened a odbc trace and I found a sql error:

DIAG [S1000] [SAP AG][LIBODBCHDB DLL][HDBODBC] General error;-10427 Conversion of parameter/column (2) from data type NVARCHAR to ASCII failed (-10427)

In this error it seems to be the chinese characteristics cannot be converted into ascii.

But why this issue appears for QSqlQueryModel? I tried to use SQLite file with chinese and QSqlQueryModel can retrieve chinese data.


Solution

  • I checked this post :link and jump to HANA ODBC connection properties document.

    It is a HANA ODBC problem. In ODBC configuration, a property item should be added and set as TRUE

    CHAR_AS_UTF8
    

    Then chinese characters will display.

    enter image description here