how to connect to sqlite database



  • i want to connect to sqlite database
    this is my code:

    QSqlDatabase Database;
    
        Database.addDatabase("QSQLITE");
        Database.setDatabaseName("C:/Users/ARASH/Desktop/arash.db");
    
        if (!Database.open())
        {
            qDebug() << Database.lastError().text();
        }
    

    but return me "Driver not loaded Driver not loaded"

    I copy qsqlite.dll in sqldrivers folder inside my app folder but don't work


  • Moderators

    @ARASHz4 That dll should be inside sqldrivers directory.
    Have a look at this wiki page.



  • This post is deleted!


  • You need to assign the connection like this:

    QSqlDatabase Database;
    
    Database = QSqlDatabase::addDatabase("QSQLITE");
    Database.setDatabaseName("C:/Users/ARASH/Desktop/arash.db");
    
    if (!Database.open())
    {
        qDebug() << Database.lastError().text();
    }


  • about how to connect to sqlite database, what's the best form to do the connection?, once tiem at start and only get connexion or connect and close on each query.
    and is possible do update with bindvalue?

    thank you, sorry for my english!


  • Qt Champions 2016

    @Yugui
    Often best design is open once at start.
    and close at end.

    • and is possible do update with bindvalue?

    yes. it is.

    QSqlQuery query;
    query.prepare("INSERT INTO person (id, forename, surname) "
                  "VALUES (:id, :forename, :surname)");
    query.bindValue(":id", 1001);
    query.bindValue(":forename", "Bart");
    query.bindValue(":surname", "Simpson");
    query.exec();
    

    http://doc.qt.io/qt-5/qsqlquery.html


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.