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. SQL & SQLQueryModel

SQL & SQLQueryModel

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.5k 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.
  • S Offline
    S Offline
    shahriar25
    wrote on 13 Apr 2016, 15:09 last edited by
    #1

    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
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 13 Apr 2016, 21:22 last edited by
      #2

      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
      0
      • S Offline
        S Offline
        shahriar25
        wrote on 14 Apr 2016, 04:11 last edited by
        #3

        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 14 Apr 2016, 05:15
        0
        • S shahriar25
          14 Apr 2016, 04:11

          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 Offline
          S Offline
          slip
          wrote on 14 Apr 2016, 05:15 last edited by
          #4

          @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
          0
          • S Offline
            S Offline
            shahriar25
            wrote on 14 Apr 2016, 06:27 last edited by
            #5

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

            1 Reply Last reply
            0

            1/5

            13 Apr 2016, 15:09

            • Login

            • Login or register to search.
            1 out of 5
            • First post
              1/5
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved