Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QDatabase error
Forum Updated to NodeBB v4.3 + New Features

QDatabase error

Scheduled Pinned Locked Moved Solved General and Desktop
3 Posts 2 Posters 959 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I dont know why, but i try to use a QDatabase for SQLite Queries. In an earlier Project everything went fine, but now i want to do a workaround to solve a problem from that earlier Project. Im trying to open and close the Database everytime if i do a Query. But i get an Error that the driver is not loaded and the database is not open. First i show my Code then the Error.

    #include <QStandardPaths>
    #include <QSqlQuery>
    #include <QSqlError>
    #include <QDir>
    #include <QDebug>
    
    gdms::Database::Database()
    {
        //Init Create Tables
        dbTables();
    
    }
    
    void gdms::Database::instanceDb(const QString &name)
    {
        QString appDataLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
        mDb = QSqlDatabase::addDatabase("QSQLITE", name);
        QDir dir(appDataLocation);
        if (!dir.exists())
            dir.mkpath(appDataLocation);
        appDataLocation.append(QString(QDir::separator()) + "gdms.sqlite");
        mDb.setDatabaseName(appDataLocation);
        if (!mDb.open())
            qDebug() << "Error open Database. " + mDb.lastError().text();
    }
    
    void gdms::Database::closeDb(const QString &name)
    {
        if (mDb.isOpen())
        {
            mDb.close();
            mDb = QSqlDatabase();
        }
        mDb.removeDatabase(name);
    }
    
    void gdms::Database::dbTables()
    {
        instanceDb("initdb");
    
        QSqlQuery query;
        query.prepare("CREATE TABLE IF NOT EXISTS treeview (id INTEGER PRIMARY KEY, index INTEGER, name TEXT)");
        if (!query.exec())
            qDebug() << "An Error occured during init/create Tables. " + query.lastError().text();
    
        closeDb("initdb");
    }
    

    and this i get when i run the app

    QSqlQuery::prepare: database not open
    "An Error occured during init/create Tables. Driver not loaded Driver not loaded"
    QSqlQuery::prepare: database not open

    Anyone knows what i do wrong? i really cant find a way to fix it. I cant create the Table in the Databasefile

    1 Reply Last reply
    0
    • the_T Offline
      the_T Offline
      the_
      wrote on last edited by
      #2

      Just a guess:

      QSqlQuery q; uses the default database connection.
      As you have set a connection name, you do not have a default connection so you have to tell the query which database to use
      use QSqlQuery q(mDb); instead.

      -- No support in PM --

      1 Reply Last reply
      2
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        thanks. that helped me.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved