[SQLite Drivers MAC OS X] - Reading works, writing does not



  • There exists a lot of posts regarding the drivers for MySQL, althought they do not treat SQLite in this manner. I will include code for forthcoming users to use.

    Using QT Creator 3.6.1 I am connecting to a database along with debug output that is succesful:

        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
         db.setDatabaseName("/Users/TheUser/Desktop/test.db");
    
        if (! db.open())
        {
           QMessageBox::critical(NULL, "Error Information", db.lastError().text());
        }
        else{
            qDebug()<<"Sucess opening the db!";
            qDebug() << "Opened" << db.isOpen() << endl;
            qDebug() << "Validity" << db.isValid() << endl;
            qDebug() << "Size" << db.tables().size() << endl;
    }
    

    OUTPUT:

    Sucess opening the db!
    Opened true 
    Validity true 
    Size 4 
    

    Next step is loading the table to a tableview, which also is succesful. Which means that the database connection works and the drivers do not mismatch (yet).

    void MainWindow::loadTable(){
        QSqlQueryModel* modal = new QSqlQueryModel();
        QSqlQuery query(db);
    
        query.prepare("SELECT * FROM Invoice");
        query.exec();
        modal->setQuery(query);
        ui->invoiceView->setModel(modal);
        ui->invoiceView->resizeColumnsToContents();
        ui->invoiceView->resizeRowsToContents();
    }
    

    Editing the table with new entries, is where I receive errors.

    void MainWindow::on_invoiceAddBtn_clicked()
    {
    
        QSqlQuery query(db);
    
        query.prepare("INSERT INTO Invoice (invoiceText, recipentName, invoiceAmount, expirationDate, invoicePayed"
                      "VALUES (:invoiceText, :recipentName, :invoiceAmount, :expirationDate, :invoicePayed)");
    
        query.bindValue(":invoiceText", ui->invoiceTextLine->text());
        query.bindValue(":recipentName", ui->recipentLine->text());
        query.bindValue(":invoiceAmount", ui->amountLine->text());
        query.bindValue(":expirationDate", ui->expirationLine->text());
        query.bindValue(":invoicePayed", ui->invoicePayedLine->text());
    
        if(!query.exec()){
            QMessageBox::information(this, "Error!", db.lastError().text());
        }
            else{
        QMessageBox::information(this, "Success adding entry!");
    }
        qDebug() <<  QSqlDatabase::drivers() ;
    }
    

    OUTPUT:

    "Driver not loaded Driver not loaded" 
    
    ("QSQLITE", "QMYSQL", "QMYSQL3", "QODBC", "QODBC3", "QPSQL", "QPSQL7")
    

    This seems to be a common error when using QT and SQL in combination. This error has made me give up QT and use other platforms before, but I would like to solve this so I can continue developing.

    What is there to do?

    Best regards.


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    Where's that db variable located ?

    From your code sample you initialize one maybe in the constructor but it would be only valid in that function.



  • @SGaist

    Thank you for the reply. I initalize it in the header file to make it accessible for all functions.

    mainwindow.h

    QSqlDatabase db;
    

  • Lifetime Qt Champion

    So from your first code snippet, you are not initializing the member variable from your class but a local one. In any case, since you have only one connection, it's not needed as it will become the default connection. You can then simply not pass any QSqlDatabase object to QSqlQuery and it will use the default connection.



  • @SGaist

    I moved

    QSqlDatabase db;
    

    from private to public and everything works fine. Thank you much for the kind replies.

    Have a great day!


  • Lifetime Qt Champion

    Like I wrote, you don't need to keep that variable. You are using only one connection so you can take advantage of the default behavior which is to use the default connection when nothing is given in parameter.


  • Lifetime Qt Champion

    By the way, since out have it working now, please mark the thread as solved using the "Topic Tool" button so that other forum users may know a solution has been found :)


Log in to reply
 

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