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. QSqlQuery::numRowsAffected() always returns 0 - why?
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery::numRowsAffected() always returns 0 - why?

Scheduled Pinned Locked Moved General and Desktop
5 Posts 3 Posters 3.9k Views 1 Watching
  • 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
    shimano
    wrote on last edited by
    #1

    Hello,

    Why numRowsAffected() always return 0?

    @
    // (...)
    QSqlDatabase mydb;
    // (...)
    mydb = QSqlDatabase::addDatabase("QSQLITE");
    mydb.setDatabaseName("./mydb.db");
    // (...)

    void MyOwnSQLite::tables()
    {
    if(mydb.databaseName().length() >= 3) {
    if(mydb.isOpen()) {
    QSqlQuery tables;
    tables.exec("SELECT name FROM sqlite_master WHERE type IN ('table')");
    qDebug() << "Query returned:" << tables.numRowsAffected() << "results";
    if(tables.numRowsAffected() < 0) {
    qDebug() << "Query error:" << tables.lastError().text();
    }
    else {
    while(tables.next()) {
    QString name = tables.value(0).toString();
    qDebug() << "\t- " << name; // display table name
    }
    }
    }
    else {
    qDebug() << "No opened database!";
    }
    }
    else {
    qDebug() << "No database object";
    }
    }
    @

    Debug:
    @
    Query returned: 0 results

    • "friends"
    • "enemies"
      @
    1 Reply Last reply
    0
    • P Offline
      P Offline
      panosk
      wrote on last edited by
      #2

      According to the doc for "numRowsAffected()":http://qt-project.org/doc/qt-5.0/qtsql/qsqlquery.html#numRowsAffected, for SELECT statements you should use "size()":http://qt-project.org/doc/qt-5.0/qtsql/qsqlquery.html#size.

      1 Reply Last reply
      0
      • S Offline
        S Offline
        shimano
        wrote on last edited by
        #3

        Ok, but size() returns -1, I guess because query is not active. So how to keep query active or how to force use numRowsAffected() in my case?

        Update:
        In Google's opinion SQLite doesn't support any size()-like methods. So my question is - Do I have to implement my own low-efficient method or is there any other better way already implemented?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          slip
          wrote on last edited by
          #4

          I've solved similar problem by using a separate query which returns number of rows. I'm sure that exists much better solutions but that variant fully satisfied me.

          @if(mydb.isOpen()) {
          QSqlQuery tables;
          int iNumOfRows=tables.exec("SELECT count(*) FROM sqlite_master WHERE type IN ('table')");
          qDebug() << "Query returned:" << iNumOfRows << "results";
          if(tables.lastError().isValid()) {
          qDebug() << "Query error:" << tables.lastError().text();
          }
          else {
          tables.exec("SELECT name FROM sqlite_master WHERE type IN ('table')");
          while(tables.next()) {
          QString name = tables.value(0).toString();
          qDebug() << "\t- " << name; // display table name
          }
          }@

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

          1 Reply Last reply
          0
          • P Offline
            P Offline
            panosk
            wrote on last edited by
            #5

            Was ready to post slip's answer :) . Use this approach, should do what you want.

            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