Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Update: Forum Guidelines & Code of Conduct

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

    General and Desktop
    2
    7
    1326
    Loading More Posts
    • 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.
    • Mauser
      Mauser last edited by

      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 Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        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

        Mauser 1 Reply Last reply Reply Quote 0
        • Mauser
          Mauser @SGaist last edited by

          @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 Reply Quote 0
          • SGaist
            SGaist Lifetime Qt Champion last edited by

            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

            Mauser 1 Reply Last reply Reply Quote 1
            • Mauser
              Mauser @SGaist last edited by

              @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 Reply Quote 0
              • SGaist
                SGaist Lifetime Qt Champion last edited by

                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 Reply Quote 0
                • SGaist
                  SGaist Lifetime Qt Champion last edited by

                  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 Reply Quote 0
                  • First post
                    Last post