QSqlQuery::prepare: no driver



  • Hello,

    I am trying to update a row in my database however when I call the following function I get an error that says "QSqlQuery::prepare: no driver".

    @void PointsModel::setCellData(int row, QString name, QString x, QString y, QString z, QString theta)
    {
    database->query->prepare("UPDATE points SET (name, x, y, z, a) VALUES (:name, :x, :y, :z, :a) WHERE id = :id");
    database->query->bindValue(":id", row);
    database->query->bindValue(":name", name);
    database->query->bindValue(":x", x);
    database->query->bindValue(":y", y);
    database->query->bindValue(":z", z);
    database->query->bindValue(":a", theta);
    database->query->exec();
    }@

    I am doing other queries to the database such as SELECT, etc. So I don't know why this doesn't work. Any thoughts???

    Thanks!



  • At a guess, the database variable is pointing to a QSqlDatabase object that has not had QSqlDatabase::addDatabase() called on it beforehand.

    Really though, you do not need to be persistently storing QSqlDatabase references or QSqlQuery objects, or allocating them on the heap in the first place.



  • [quote author="ChrisW67" date="1391220132"]At a guess, the database variable is pointing to a QSqlDatabase object that has not had QSqlDatabase::addDatabase() called on it beforehand.

    Really though, you do not need to be persistently storing QSqlDatabase references or QSqlQuery objects, or allocating them on the heap in the first place.[/quote]

    I did call addDatabase() in the constructor. I did the following:

    @ db = QSqlDatabase::addDatabase( "QSQLITE" );
    db.setDatabaseName( "../database" );

    if ( !db.open() )
    {
        qDebug() << "Failed to connect to database: " + db.lastError().text();
        return false;
    }
    
    qDebug() << "Successfully connected to database...";
    
    query = new QSqlQuery(db);@


  • Just as an a test I implemented the following line before my queries to "re-register" the driver:

    @database->query = new QSqlQuery(database->db);@

    It got rid of the no driver error, however having to make a new query for each call I make doesnt seem right. Also, When looking into the database, it does not see to add anything to it, even if I call commit.



  • @rizoritis i noticed that in qt: if you setdatabaseName and give it a correct path but with an incorrect database-name; qt creates an empty database and when you try to retrieve data in that database and because it is empty qt gives the error QSqlQuery::prepare: no driver: in short if you have an already existing database make sure you spell it correctly otherwise first create tables and then insert data in order to retrieve, update etc.


Log in to reply
 

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