SQL & SQLQueryModel



  • 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


  • Lifetime Qt Champion

    Hi,

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

    Hope it helps



  • 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?



  • @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 ;)



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


Log in to reply
 

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