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. [SQLite Drivers MAC OS X] - Reading works, writing does not
QtWS25 Last Chance

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
7 Posts 2 Posters 1.7k Views
  • 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.
  • MauserM Offline
    MauserM Offline
    Mauser
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      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.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      MauserM 1 Reply Last reply
      0
      • SGaistS SGaist

        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.

        MauserM Offline
        MauserM Offline
        Mauser
        wrote on last edited by
        #3

        @SGaist

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

        mainwindow.h

        QSqlDatabase db;
        
        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          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.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          MauserM 1 Reply Last reply
          1
          • SGaistS SGaist

            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.

            MauserM Offline
            MauserM Offline
            Mauser
            wrote on last edited by
            #5

            @SGaist

            I moved

            QSqlDatabase db;
            

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

            Have a great day!

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              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.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                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 :)

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                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