Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

SQLITE with QT Application...



    1. How to add SQLITE suporte to my application?
    2. I will need to compile the SQLITE driver?
    3. Have any step-by-step example?

    Thanks everyone.





  • Hi
    do not have to compile the SQLite driver.

    simple example:

    in *.pro file add line
    @
    QT += sql
    @
    and
    @
    #include <QtSql/QtSql>
    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("fileName.db3");
    db.open();
    // db.close(); // for close connection
    @

    more, depending on the task.



  • Thanks!

    Skyrim, where you find this example?



  • Another question...
    I'm trying to open my generated database with some tables on SQLITEMAN and the SQLITEMAN does not show any table on the file...
    I have used sqlite on android apps and I could open the SQLITE FILES on SQLITEMAN withtout any problems....

    It's a problem, it could not be done... anyone with the same "problem"?
    thanks



    • different sqlite versions?
    • did you at least generate any table in the database (qsqlquery, qsqltablemodel, ...)?

    ;-) .. frank



  • Could be the version, I read something that says Sqliteman just open sqlite3 files...

    I make this code, case study code?

    @
    #include "banco.h"
    #include "QtSql/QtSql"
    #include "QDebug"

    Banco::Banco(QObject *parent) :
    QObject(parent)
    {
    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName( QDir::homePath() + QDir::separator() + "inoveDB.db3");
    if (db.open()) {
    QSqlQuery query(db);
    if (query.exec("CREATE TABLE usuario ( INTEGER oid PRIMARY KEY);")) {
    qDebug() << "criado tabela com sucesso.";
    } else {
    qDebug() << "erro ao criar tabela." << query.lastError();
    }
    } else {
    qDebug() << "DB Nao aberto.";
    }
    db.close();
    @

    • brazillian portuguese debug messages.

    If I run this code twice, I got error saying that already exists the table "usuario".



  • Hmm, code looks quite good. Have you tried the Firefox Plugin "SQLite Manager"? Sqlite3 is Qt-default as far as I know.



  • I read the documentation, and the Qt uses Sqlite2 only if you include the sqlite2, by default really appers to be sqlite3.
    I will try try Sqlite Manager.



  • With Firefox Plugin show my "usuario" table! So my code works!
    Another detail: Sqliteman uses QT!



  • Can someone give me and example or complete code of an app with sqlite please?


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Please don't revive old thread with new questions, open your own.

    As for your question, have a look at the "Sql Example in Qt's documentation":http://qt-project.org/doc/qt-4.8/examples-sql.html



  • Its working. Just SQL part you using will generate error second time becouse table already exists. You could replace with CREATE TABLE IF NOT EXISTS ....

    [quote author="dcbasso" date="1341519956"]Could be the version, I read something that says Sqliteman just open sqlite3 files...

    I make this code, case study code?

    @
    #include "banco.h"
    #include "QtSql/QtSql"
    #include "QDebug"

    Banco::Banco(QObject *parent) :
    QObject(parent)
    {
    QSqlDatabase db;
    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName( QDir::homePath() + QDir::separator() + "inoveDB.db3");
    if (db.open()) {
    QSqlQuery query(db);
    if (query.exec("CREATE TABLE usuario ( INTEGER oid PRIMARY KEY);")) {
    qDebug() << "criado tabela com sucesso.";
    } else {
    qDebug() << "erro ao criar tabela." << query.lastError();
    }
    } else {
    qDebug() << "DB Nao aberto.";
    }
    db.close();
    @

    • brazillian portuguese debug messages.

    If I run this code twice, I got error saying that already exists the table "usuario".[/quote]


Log in to reply