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 query statement limit? Can't delete from table when the query statement is very long (Sqlite)
Forum Updated to NodeBB v4.3 + New Features

QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite)

Scheduled Pinned Locked Moved Solved General and Desktop
21 Posts 6 Posters 2.4k Views 2 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.
  • H Offline
    H Offline
    hbatalha
    wrote on last edited by
    #1

    I am using Sqlite on my app where I need to delete from a table where the primary key is a obscured CHAR that created with QByteArray::toBase64

    void insertIntoTable(QString key)
    {
        QSqlQuery query;
    
        query.prepare("INSERT INTO download (key) VALUES(:key)");
    
        QByteArray obscuredStr = key.toUtf8();
        obscuredStr = obscuredStr.toBase64();
        
        query.bindValue(":key", obscuredStr);
    }
    
    void DatabaseManager::deleteFromTable(QString table, QString columnName, QString valueToCompareAgainst)
    {
        QSqlQuery query;
    
        QByteArray obscuredStr = valueToCompareAgainst.toUtf8();
        obscuredStr = obscuredStr.toBase64();
    
        if( ! query.exec("DELETE FROM " + table + " WHERE " + columnName + "= '" + obscuredStr+ "'" ))
        {
            LOG_ERROR("SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text());
            qDebug() << "SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text();
        }
    }
    

    The code runs fine at the insertion but it can't seem to delete data from the table. It doesn't return any error. It just won't delete. If I I don't use the pseudo-encryption with toBase64, which turns the stament a lot less long, it deletes that data.
    I have not idea what I am might be missing.

    jsulmJ JonBJ 2 Replies Last reply
    0
    • H hbatalha

      I am using Sqlite on my app where I need to delete from a table where the primary key is a obscured CHAR that created with QByteArray::toBase64

      void insertIntoTable(QString key)
      {
          QSqlQuery query;
      
          query.prepare("INSERT INTO download (key) VALUES(:key)");
      
          QByteArray obscuredStr = key.toUtf8();
          obscuredStr = obscuredStr.toBase64();
          
          query.bindValue(":key", obscuredStr);
      }
      
      void DatabaseManager::deleteFromTable(QString table, QString columnName, QString valueToCompareAgainst)
      {
          QSqlQuery query;
      
          QByteArray obscuredStr = valueToCompareAgainst.toUtf8();
          obscuredStr = obscuredStr.toBase64();
      
          if( ! query.exec("DELETE FROM " + table + " WHERE " + columnName + "= '" + obscuredStr+ "'" ))
          {
              LOG_ERROR("SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text());
              qDebug() << "SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text();
          }
      }
      

      The code runs fine at the insertion but it can't seem to delete data from the table. It doesn't return any error. It just won't delete. If I I don't use the pseudo-encryption with toBase64, which turns the stament a lot less long, it deletes that data.
      I have not idea what I am might be missing.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @hbatalha Please first take a look at documentation to see how to properly construct SQL queries in Qt: https://doc.qt.io/qt-5/qsqlquery.html
      Hint: use prepare().

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      H 1 Reply Last reply
      2
      • H hbatalha

        I am using Sqlite on my app where I need to delete from a table where the primary key is a obscured CHAR that created with QByteArray::toBase64

        void insertIntoTable(QString key)
        {
            QSqlQuery query;
        
            query.prepare("INSERT INTO download (key) VALUES(:key)");
        
            QByteArray obscuredStr = key.toUtf8();
            obscuredStr = obscuredStr.toBase64();
            
            query.bindValue(":key", obscuredStr);
        }
        
        void DatabaseManager::deleteFromTable(QString table, QString columnName, QString valueToCompareAgainst)
        {
            QSqlQuery query;
        
            QByteArray obscuredStr = valueToCompareAgainst.toUtf8();
            obscuredStr = obscuredStr.toBase64();
        
            if( ! query.exec("DELETE FROM " + table + " WHERE " + columnName + "= '" + obscuredStr+ "'" ))
            {
                LOG_ERROR("SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text());
                qDebug() << "SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text();
            }
        }
        

        The code runs fine at the insertion but it can't seem to delete data from the table. It doesn't return any error. It just won't delete. If I I don't use the pseudo-encryption with toBase64, which turns the stament a lot less long, it deletes that data.
        I have not idea what I am might be missing.

        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #3

        @hbatalha

        • Change your DELETE statement to be same with key but as a SELECT instead. That eliminates whether the issue has anything to do with deleting.

        • Use the returned column from this SELECT statement to compare against the valueToCompareAgainst and/or the original key. Try to see if you can see a difference there.

        • Instead of '" + obscuredStr+ "'" try passing that as a bound variable to the DELETE statement, as you pass it into the INSERT statement.

        In your INSERT statement you do not actually exec() anything. So if that is your real code you never put the item into the table. Which might explain why you can't delete it.....

        H 1 Reply Last reply
        2
        • jsulmJ jsulm

          @hbatalha Please first take a look at documentation to see how to properly construct SQL queries in Qt: https://doc.qt.io/qt-5/qsqlquery.html
          Hint: use prepare().

          H Offline
          H Offline
          hbatalha
          wrote on last edited by
          #4

          @jsulm said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):

          Hint: use prepare().

          I have tried this :

           query.prepare("DELETE FROM " + table + " WRERE " + columnName + "= '(:key)'");
              query.bindValue(":key", valueToCompareAgainst);
          
              //  query.exec();
              if (! query.exec())
              {
                  LOG_ERROR("SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text());
                  qDebug() << "SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text();
              }
          

          Gives me the error : No query Unable to fetch row

          KroMignonK 1 Reply Last reply
          0
          • JonBJ JonB

            @hbatalha

            • Change your DELETE statement to be same with key but as a SELECT instead. That eliminates whether the issue has anything to do with deleting.

            • Use the returned column from this SELECT statement to compare against the valueToCompareAgainst and/or the original key. Try to see if you can see a difference there.

            • Instead of '" + obscuredStr+ "'" try passing that as a bound variable to the DELETE statement, as you pass it into the INSERT statement.

            In your INSERT statement you do not actually exec() anything. So if that is your real code you never put the item into the table. Which might explain why you can't delete it.....

            H Offline
            H Offline
            hbatalha
            wrote on last edited by
            #5

            @JonB

            Use the returned column from this SELECT statement to compare against the valueToCompareAgainst and/or the original key

            like this?

                QSqlQuery query;
                query.prepare("DELETE FROM " + table + " WRERE " + columnName + "= ?");
                query.addBindValue(valueToCompareAgainst);
                query.exec();
            
                qDebug() << (valueToCompareAgainst ==  query.boundValue(0).toString()); // returns true
            

            It returns true when comaparing them. But I can't pass it to DELETE, can't figure out how.

            In your INSERT statement you do not actually exec() anything. So if that is your real code you never put the item into the table. Which might explain why you can't delete it.....

            No it is not my real code. I forgot about it when type that minimal example.

            JonBJ 1 Reply Last reply
            0
            • H hbatalha

              @jsulm said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):

              Hint: use prepare().

              I have tried this :

               query.prepare("DELETE FROM " + table + " WRERE " + columnName + "= '(:key)'");
                  query.bindValue(":key", valueToCompareAgainst);
              
                  //  query.exec();
                  if (! query.exec())
                  {
                      LOG_ERROR("SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text());
                      qDebug() << "SQL QUERY ERROR: Failed to delete from " + table + " : " + query.lastError().text();
                  }
              

              Gives me the error : No query Unable to fetch row

              KroMignonK Offline
              KroMignonK Offline
              KroMignon
              wrote on last edited by
              #6

              @hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):

              WRERE

              I think this should be WHERE ;)

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              H 1 Reply Last reply
              0
              • KroMignonK KroMignon

                @hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):

                WRERE

                I think this should be WHERE ;)

                H Offline
                H Offline
                hbatalha
                wrote on last edited by
                #7

                @KroMignon oops, I mistyped. But still it doesn't delete from the table.
                If I don't obscure the string it deletes fine. It does look if the statement lenght is less long it works.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #8

                  Hi,

                  Print the query that was generated to see if there's something wrong there.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  H 1 Reply Last reply
                  0
                  • SGaistS SGaist

                    Hi,

                    Print the query that was generated to see if there's something wrong there.

                    H Offline
                    H Offline
                    hbatalha
                    wrote on last edited by
                    #9

                    @SGaist

                    QSqlQuery query;
                        query.prepare("DELETE FROM " + table + " WHERE " + columnName + "= ?");
                        query.addBindValue(valueToCompareAgainst);
                        query.exec();
                    
                       qDebug() << query.lastQuery(); // returns "DELETE FROM tableName WHERE columnName= ?"
                    
                        qDebug() << (valueToCompareAgainst ==  query.boundValue(0).toString()); // returns true
                    

                    The query generated seems to be ok.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #10

                      Can you show it anyway ? Looking OK and being OK can be subtly different.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      H 1 Reply Last reply
                      0
                      • H hbatalha

                        @JonB

                        Use the returned column from this SELECT statement to compare against the valueToCompareAgainst and/or the original key

                        like this?

                            QSqlQuery query;
                            query.prepare("DELETE FROM " + table + " WRERE " + columnName + "= ?");
                            query.addBindValue(valueToCompareAgainst);
                            query.exec();
                        
                            qDebug() << (valueToCompareAgainst ==  query.boundValue(0).toString()); // returns true
                        

                        It returns true when comaparing them. But I can't pass it to DELETE, can't figure out how.

                        In your INSERT statement you do not actually exec() anything. So if that is your real code you never put the item into the table. Which might explain why you can't delete it.....

                        No it is not my real code. I forgot about it when type that minimal example.

                        JonBJ Online
                        JonBJ Online
                        JonB
                        wrote on last edited by JonB
                        #11

                        @hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):

                        @JonB

                        Use the returned column from this SELECT statement to compare against the valueToCompareAgainst and/or the original key

                        like this?

                        No,

                        SELECT [columnname] FROM [table] WHERE [columnname] = '<value>`
                        

                        If that doesn't return the row it's not surprising it can't be deleted.

                        SELECT [columnname] FROM [table]
                        

                        Compare the column value you get back for the row you want to delete against what you are passing for its value to DELETE FROM [table] WHERE [columnname] = '<value>'.

                        All just trying to help you to debug.

                        H 1 Reply Last reply
                        0
                        • SGaistS SGaist

                          Can you show it anyway ? Looking OK and being OK can be subtly different.

                          H Offline
                          H Offline
                          hbatalha
                          wrote on last edited by
                          #12

                          @SGaist I thought that is the way to do it. So how do I print the query that was generated ?

                          SGaistS 1 Reply Last reply
                          0
                          • JonBJ JonB

                            @hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):

                            @JonB

                            Use the returned column from this SELECT statement to compare against the valueToCompareAgainst and/or the original key

                            like this?

                            No,

                            SELECT [columnname] FROM [table] WHERE [columnname] = '<value>`
                            

                            If that doesn't return the row it's not surprising it can't be deleted.

                            SELECT [columnname] FROM [table]
                            

                            Compare the column value you get back for the row you want to delete against what you are passing for its value to DELETE FROM [table] WHERE [columnname] = '<value>'.

                            All just trying to help you to debug.

                            H Offline
                            H Offline
                            hbatalha
                            wrote on last edited by hbatalha
                            #13

                            @JonB What about now?

                            QSqlQuery query;
                                if(! query.exec("SELECT * FROM " + table + " WHERE " + columnName + "= '" + valueToCompareAgainst + "'"))
                                {
                                    qDebug() << "SQL QUERY ERROR: " + table + " : " + query.lastError().text();
                                }
                            
                                while(query.next())
                                    qDebug() << "RETURNED: " << query.value(columnName).toString();
                            

                            This does not return anything. Might the error be here? What should I do?

                            All just trying to help you to debug.

                            I really appreciate it

                            1 Reply Last reply
                            0
                            • H hbatalha

                              @SGaist I thought that is the way to do it. So how do I print the query that was generated ?

                              SGaistS Offline
                              SGaistS Offline
                              SGaist
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):

                              @SGaist I thought that is the way to do it. So how do I print the query that was generated ?

                              My bad ! I misread the code part.

                              One thing that might be wrong is the missing space between the column name and the question mark.

                              By the way, which version of Qt are you using ?

                              Interested in AI ? www.idiap.ch
                              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                              H 1 Reply Last reply
                              0
                              • SGaistS SGaist

                                @hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):

                                @SGaist I thought that is the way to do it. So how do I print the query that was generated ?

                                My bad ! I misread the code part.

                                One thing that might be wrong is the missing space between the column name and the question mark.

                                By the way, which version of Qt are you using ?

                                H Offline
                                H Offline
                                hbatalha
                                wrote on last edited by
                                #15

                                @SGaist

                                One thing that might be wrong is the missing space between the column name and the question mark.

                                Where? You mean the space between columnName and the '=' sign.

                                By the way, which version of Qt are you using ?

                                Qt Creator IDE version? If so, it is Qt 4.15.1

                                jsulmJ 1 Reply Last reply
                                0
                                • H hbatalha

                                  @SGaist

                                  One thing that might be wrong is the missing space between the column name and the question mark.

                                  Where? You mean the space between columnName and the '=' sign.

                                  By the way, which version of Qt are you using ?

                                  Qt Creator IDE version? If so, it is Qt 4.15.1

                                  jsulmJ Online
                                  jsulmJ Online
                                  jsulm
                                  Lifetime Qt Champion
                                  wrote on last edited by
                                  #16

                                  @hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):

                                  Qt Creator IDE version? If so, it is Qt 4.15.1

                                  No, Qt version.
                                  QtCreator is an IDE, Qt is a C++ framework.
                                  What you posted is the QtCreator version, question was which Qt version you're using.

                                  https://forum.qt.io/topic/113070/qt-code-of-conduct

                                  H 1 Reply Last reply
                                  0
                                  • jsulmJ jsulm

                                    @hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):

                                    Qt Creator IDE version? If so, it is Qt 4.15.1

                                    No, Qt version.
                                    QtCreator is an IDE, Qt is a C++ framework.
                                    What you posted is the QtCreator version, question was which Qt version you're using.

                                    H Offline
                                    H Offline
                                    hbatalha
                                    wrote on last edited by
                                    #17

                                    @jsulm @SGaist Then it is Qt 6.0.3

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

                                      What qsql driver do you use? Can you reproduce it with a in-memory sqlite database so we can try to check it on our systems?

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

                                      H 1 Reply Last reply
                                      0
                                      • Christian EhrlicherC Christian Ehrlicher

                                        What qsql driver do you use? Can you reproduce it with a in-memory sqlite database so we can try to check it on our systems?

                                        H Offline
                                        H Offline
                                        hbatalha
                                        wrote on last edited by
                                        #19

                                        @Christian-Ehrlicher said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):

                                        an you reproduce it with a in-memory sqlite database so we can try to check it on our systems?

                                        Sorry, I don't know how to do that yet. I am a SQL newbie, just started learning it few weeks ago in college. I believe you mean something like this.

                                        What qsql driver do you use?

                                        QSQLITE

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

                                          @hbatalha said in QSqlQuery query statement limit? Can't delete from table when the query statement is very long (Sqlite):

                                          Sorry, I don't know how to do that yet. I am a SQL newbie, just started learning it few weeks ago in college

                                          Don't see why this hinders you from creating a minimal, compilable reproducer for us.

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

                                          H 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