Solved SQLite BLOB to QString
-
I have a SQLite database with a field typed
BLOB
(Binary),
but this field contains normal text.
I want to have this field value asQString
.
I already was able to get the value asByteArray
.Here is my current code:
QSqlQuery query("SELECT value FROM Tabelle WHERE key='token'", db); query.first(); QString data= query.value(0).toString();
But what I get is:
"\""
-
Hi
Did you try to convert to string via ByteArray ?
Here you do directly from QVariant -
How do I do this?
I searched for it but I couldn't find a way to do this viaQVariant
Edit: Yes, I did, the SQLite result is a
QVariant
-
@CryptixDE
Hi
did not check if even possible but i meant like
QByteArray data= query.value(0).toByteArray();
QString datastr(data); -
This will depend a lot on the actual data that is present in the BLOB column ("text" can mean many many different things at the binary level).
But for a more generic case, something like this should work:
QByteArray strData = query.value(0).toByteArray(); QString str = QString::fromUtf8(strData);
-
-
You need to know the encoding of your string (I assumed UTF-8 but if you are getting only the quotation marks it's most likely UTF-16) and use the appropriate QString constructor or from**** static method.
-
@fsilva so
fromUtf16
instead offromUtf8
should work? -
Yea, I solved this issue by using the hex value