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 exec returns false
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery exec returns false

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 3 Posters 597 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.
  • Y Offline
    Y Offline
    Yihua Liu
    wrote on last edited by
    #1
    const QSqlDatabase db = QSqlDatabase::database(conn_name, false);
    if (not db.isValid()) {
        qWarning() << "Database is invalid.";
        return;
    }
    if (not db.isOpen()) {
        qWarning() << "Database not open.";
        return;
    }
    QSqlQuery table_query(db);
    if (const bool prepared = table_query.prepare(QString("SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER=:owner")); not prepared) {
        qWarning() << "Failed to prepare SELECT TABLE_NAME query:" << table_query.lastError();
    }
    table_query.bindValue(":owner", db.userName().toUpper());
    table_query.exec();  // set active if return true
    if (table_query.isActive()) {
        qWarning() << "Failed to execute SELECT TABLE_NAME query:" << table_query.lastError();
        return;
    }
    

    db is valid and is open, and the query is prepared, but exec() returns false and the query is inactive at last.
    If I change the prepared statement to a direct exec("...") statement, the error still occurs.
    The output is

    Failed to execute SELECT TABLE_NAME query: QSqlError("", "", "")

    It is weird that the QSqlError is empty so I cannot figure out what is wrong. I also tried to output db.lastError() but it is also an empty QSqlError.
    I run the SQL query in my Oracle client and it can properly output all the selected TABLE_NAME items.
    I'm using Qt 6.8.0 and Oracle 21.3 client.

    JonBJ 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #6

      Then without a prepared query.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      Y 1 Reply Last reply
      0
      • Y Yihua Liu
        const QSqlDatabase db = QSqlDatabase::database(conn_name, false);
        if (not db.isValid()) {
            qWarning() << "Database is invalid.";
            return;
        }
        if (not db.isOpen()) {
            qWarning() << "Database not open.";
            return;
        }
        QSqlQuery table_query(db);
        if (const bool prepared = table_query.prepare(QString("SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER=:owner")); not prepared) {
            qWarning() << "Failed to prepare SELECT TABLE_NAME query:" << table_query.lastError();
        }
        table_query.bindValue(":owner", db.userName().toUpper());
        table_query.exec();  // set active if return true
        if (table_query.isActive()) {
            qWarning() << "Failed to execute SELECT TABLE_NAME query:" << table_query.lastError();
            return;
        }
        

        db is valid and is open, and the query is prepared, but exec() returns false and the query is inactive at last.
        If I change the prepared statement to a direct exec("...") statement, the error still occurs.
        The output is

        Failed to execute SELECT TABLE_NAME query: QSqlError("", "", "")

        It is weird that the QSqlError is empty so I cannot figure out what is wrong. I also tried to output db.lastError() but it is also an empty QSqlError.
        I run the SQL query in my Oracle client and it can properly output all the selected TABLE_NAME items.
        I'm using Qt 6.8.0 and Oracle 21.3 client.

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by JonB
        #2

        @Yihua-Liu said in QSqlQuery exec returns false:

        If I change the prepared statement to a direct exec("...") statement, the error still occurs.

        So I think you are saying

        table_query.exec("SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER='owner'")
        

        fails? Try printing table_query.lastError() immediately after the exec(), not table_query.isActive(), just in case that interferes.

        Now try just "SELECT * FROM ALL_TABLES", no WHERE or OWNER. If that too fails see if you can execute any SELECT statement, not against a special table like ALL_TABLES. Just e.g. a SELECT 0 is a legal SQL statement!

        At the end of this you should know whether you cannot execute any statements at all, you have trouble with ALL_TABLES or you have trouble with binding.

        Y 1 Reply Last reply
        1
        • JonBJ JonB

          @Yihua-Liu said in QSqlQuery exec returns false:

          If I change the prepared statement to a direct exec("...") statement, the error still occurs.

          So I think you are saying

          table_query.exec("SELECT TABLE_NAME FROM ALL_TABLES WHERE OWNER='owner'")
          

          fails? Try printing table_query.lastError() immediately after the exec(), not table_query.isActive(), just in case that interferes.

          Now try just "SELECT * FROM ALL_TABLES", no WHERE or OWNER. If that too fails see if you can execute any SELECT statement, not against a special table like ALL_TABLES. Just e.g. a SELECT 0 is a legal SQL statement!

          At the end of this you should know whether you cannot execute any statements at all, you have trouble with ALL_TABLES or you have trouble with binding.

          Y Offline
          Y Offline
          Yihua Liu
          wrote on last edited by Yihua Liu
          #3

          @JonB Thanks. If I print table_query.lastError() immediately after exec(), the QSqlError is still empty.
          SELECT 0 has no problem, but SELECT * FROM ALL_TABLES still triggers the problem. However, it still confuses me. I read QStringList QOCIDriver::tables(QSql::TableType type) const function in qsql_oci.cpp:

          const auto tableQuery = "select owner, table_name from all_tables where "_L1;
          const QString where = make_where_clause(user, AndExpression);
          t.exec(tableQuery + where);
          

          If I write db.tables(), my program can successfully read all the table names, so to select from all_tables seems to be no problem. Are there any additional special steps to read all_tables from an Oracle database by QOCI driver?

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #4

            Case sensitive database maybe?

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            JonBJ 1 Reply Last reply
            0
            • Christian EhrlicherC Christian Ehrlicher

              Case sensitive database maybe?

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #5

              @Christian-Ehrlicher
              I wondered about that, but https://docs.oracle.com/en/database/oracle/oracle-database/21/refrn/ALL_TABLES.html gives the names in all capitals and the OP claims the query (as-is, I presume!) works

              I run the SQL query in my Oracle client and it can properly output all the selected TABLE_NAME items.

              1 Reply Last reply
              0
              • Christian EhrlicherC Offline
                Christian EhrlicherC Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on last edited by
                #6

                Then without a prepared query.

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                Y 1 Reply Last reply
                0
                • Christian EhrlicherC Christian Ehrlicher

                  Then without a prepared query.

                  Y Offline
                  Y Offline
                  Yihua Liu
                  wrote on last edited by
                  #7

                  @Christian-Ehrlicher Hello, without a prepared query the problem still occurs. Besides, if I try to exec something like

                  SELECT SOME_FIELD_NAME FROM SOME_TABLE_NAME
                  

                  the problem also occurs

                  JonBJ 1 Reply Last reply
                  0
                  • Y Yihua Liu

                    @Christian-Ehrlicher Hello, without a prepared query the problem still occurs. Besides, if I try to exec something like

                    SELECT SOME_FIELD_NAME FROM SOME_TABLE_NAME
                    

                    the problem also occurs

                    JonBJ Offline
                    JonBJ Offline
                    JonB
                    wrote on last edited by
                    #8

                    @Yihua-Liu
                    Since I asked and you said SELECT 0 does not even work you can forget everything else.

                    I suspect you now need someone else to try with your version of Qt and of the Oracle client. If you can try a different version of Qt and/or a different version of Oracle client (I don't know how that works) that would be great. Otherwise you had better say exactly how you got your Qt (e.g. did you compile it yourself? with what config?) and similarly for whatever the Qt-to-Oracle library/driver is?

                    Y 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Yihua-Liu
                      Since I asked and you said SELECT 0 does not even work you can forget everything else.

                      I suspect you now need someone else to try with your version of Qt and of the Oracle client. If you can try a different version of Qt and/or a different version of Oracle client (I don't know how that works) that would be great. Otherwise you had better say exactly how you got your Qt (e.g. did you compile it yourself? with what config?) and similarly for whatever the Qt-to-Oracle library/driver is?

                      Y Offline
                      Y Offline
                      Yihua Liu
                      wrote on last edited by
                      #9
                      This post is deleted!
                      1 Reply Last reply
                      0
                      • Y Yihua Liu has marked this topic as solved on

                      • Login

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