Qt Forum

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

    Update: Forum Guidelines & Code of Conduct

    Solved SQL & SQLQueryModel

    General and Desktop
    3
    5
    1337
    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.
    • S
      shahriar25 last edited by

      Hi,
      i'm learning how to work with sql in qt. so I do this:

      sqlQueryTable->exec("BEGIN TRANSACTION", false);
      
      sqlQueryTable->query()->prepare("INSERT INTO Pictures VALUES(?,?,?,?)");
      
      LoadProgress progress(0,0,this);
      progress.show();
      QCoreApplication::processEvents();
      
      QVariantList ids;
      QVariantList filePaths;
      QVariantList paths;
      QVariantList fileNames;
      
      for (int i=0; i<fileList.count(); ++i)
      {
          ids.append(i);
          paths.append(QFileInfo(fileList[i]).absolutePath());
          filePaths.append(QFileInfo(fileList[i]).absoluteFilePath());
          fileNames.append(QFileInfo(fileList[i]).fileName());
      }
      
      sqlQueryTable->query()->addBindValue(ids);
      sqlQueryTable->query()->addBindValue(fileNames);
      sqlQueryTable->query()->addBindValue(filePaths);
      sqlQueryTable->query()->addBindValue(paths);
      sqlQueryTable->query()->execBatch();
      
      sqlQueryTable->exec("COMMIT");
      

      sqlQueryTable is a class I created to access easier to QSqlQueryModel and QSqlQuery. so when I do this and get the rowCount() of the model it shows 256 but when I set a QListView's model to this model and scroll down to the last row and the get the rowCount() of the model It shows 9267 (the correct number). how can I get the rowCount right without having to scroll the listView?
      Thanks in advance

      1 Reply Last reply Reply Quote 0
      • SGaist
        SGaist Lifetime Qt Champion last edited by

        Hi,

        Depending on your database backend QSqlQuery::size might be useful.

        Hope it helps

        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
        • S
          shahriar25 last edited by

          Hi @SGaist
          thanks for helping but I don't just want to get the size I also want to get the values of the QSQlRecords that are in the model but the rowCount() doesn't show the correct size and also when I try to get a record in a row out of the rowCount() range the program crashe.
          what should I do?

          S 1 Reply Last reply Reply Quote 0
          • S
            slip @shahriar25 last edited by

            @shahriar25
            it's quite detail described in wiki.

            "If the database supports returning the size of a query (see QSqlDriver::hasFeature()), the number of rows of the current query is returned. Otherwise, returns the number of rows currently cached on the client"

            Obviously that your database doesn't support returning the size of a query.

            QSqlQueryModel::fetchMore will help you ;)

            There are 10 types of people in the world... those who understand binary and those who don't.

            1 Reply Last reply Reply Quote 0
            • S
              shahriar25 last edited by

              Hi @slip
              thanks I did't see that whole cached thing. thank you the problem is solved

              1 Reply Last reply Reply Quote 0
              • First post
                Last post