Important: Please read the Qt Code of Conduct -

Save a QString to sqlite and fetch it back?

  • Hello guys,

    I am writing an rss reader. I need a standalone database for storage. I chose sqlite, but I have problem saving QStrings to my database.

    here is how I save a QString to my sqlite database:

    QByteArray qba;

        bool bindresult=sqlite3_bind_text(insertASubscriptionStatement,2,qba.constData(), qba.count(), 0);


    And this is how i get it back:

    QString title=QString().fromUtf8((char*)sqlite3_column_text(fetchSubscriptionListStatement, 1));

    this works well if the QString has only english characters, but messes up if it has Chinese characters. All the Chinese characters become ?? after reading back.

    It looks like an encoding problem. I have tried different conversions, but I couldn't make it work.

    If you have experience using Qt with sqlite, could you please show me an example?

    Thank you very much!

  • I would recommend to use Qt's SQL classes for storing and retrieving data. They should handle the conversion pretty good and you can operate on QStrings directly. The SQLite driver of Qt should be available with all binary installations.

Log in to reply