Printing non-ascii characters.
Hi,
after a quest for connecting to an odbc data source and after much documentation reading, i finally have a snippet that i can connect and execute a parameterized stored procedure! :)
The sp is a simple one, getting an integer and returning one colymn of string data
The only 2 questions i have is that if i HAVE to declare the stored procedure parameter type as QVariant(4) or is another way to do this,
and (more important) how i can print non-ascii data that exists in the result set...
I am using Eclipse/PyDev (if that matters) but i print garbage from the snipet below,
and am not sure if the i correctly coded the part about QVariant.toString and QtCore.QString.toUtf8(candidate). (i am propably not...)
In a real app would put the data in a grid but what about now ?? I check the variable"candidate" in the debugger and it correctly contains the Greek characters that came from the database, but how i can...print them ??:confused:
Is there something in Qt that i am not doing (or at all...) correcltly ?
Code is below:
Code:
# -*- coding: utf-8 -*-
import sys
from PyQt4 import QtCore, QtSql, QtGui
db.setDatabaseName("pm");
f = db.open()
if f:
print "Connected"
query.prepare("{call graduation_fetch(?)}")
query.setForwardOnly(True)
query.
bindValue(0, QtCore.
QVariant(4), QtSql.
QSql.
In) query.exec_()
while query.next():
candidate
= QtCore.
QVariant.
toString(query.
value(0)) print QtCore.
QString.
toUtf8(candidate
)
db.close()
and of course, thanks for any help!
Re: Printing non-ascii characters.
Quote:
Originally Posted by
kikapu
while query.next():
candidate = QtCore.QVariant.toString(query.value(0))
print QtCore.QString.toUtf8(candidate)
It should be:
Code:
while query.next():
candidate = query.value(0).toString()
print candidate.toUtf8()
but those strings will be displayed correctly only if your console uses UTF-8. You might try toAscii() or toLocal8Bit() instead.
Re: Printing non-ascii characters.
Quote:
Originally Posted by
jacek
It should be:
Code:
while query.next():
candidate = query.value(0).toString()
print candidate.toUtf8()
Hmm...so, i correctly did not like that QVariant stuff, but i use Python as you already know so the back-and-forth of the C++ documentations is not every time so straightforward ;)
Quote:
but those strings will be displayed correctly only if your console uses UTF-8. You might try toAscii() or toLocal8Bit() instead.
Thanks a lot! That toLocal8Bit() did the trick! The Greek names are now correctly displayed to the Console Window.
So, i already did a form by using the Qt Designer and i can display the same data to a listWidget.
Thanks again! :)