Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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



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


  • Lifetime Qt Champion

    @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().



  • @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.....



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



  • @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.





  • @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.


  • Lifetime Qt Champion

    Hi,

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



  • @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.


  • Lifetime Qt Champion

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



  • @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.



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



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


  • Lifetime Qt Champion

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



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


  • Lifetime Qt Champion

    @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.



  • @jsulm @SGaist Then it is Qt 6.0.3


  • Lifetime Qt Champion

    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?



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


  • Lifetime Qt Champion

    @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.



  • @Christian-Ehrlicher @jsulm @JonB @SGaist @KroMignon

    I think I solved the problem! When I was inserting that obscured CHAR, the type I was using was QByteArray and when trying to delete the type was a QString. Even though they contained the same characters something I don't know what yet was stopping it from getting deleted.

    .h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    #include <QtSql>
    
    QT_BEGIN_NAMESPACE
    namespace Ui {
    class MainWindow;
    }
    QT_END_NAMESPACE
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        MainWindow(QWidget *parent = nullptr);
        ~MainWindow();
    
    private slots:
        void on_add_clicked();
    
        void on_delete_2_clicked();
    
    private:
        Ui::MainWindow *ui;
    
        QSqlDatabase d_base;
    };
    #endif // MAINWINDOW_H
    
    

    .cpp

    MainWindow::MainWindow(QWidget *parent)
        : QMainWindow(parent)
        , ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
    
        d_base = QSqlDatabase::addDatabase("QSQLITE");
        d_base.setDatabaseName("database.sqlite");
    
        if(!d_base.open())
        {
            qDebug() << "Database failed to open : " + d_base.lastError().text();
        }
    
        QSqlQuery query;
    
        bool success = false;
    
        success = query.exec("CREATE TABLE IF NOT EXISTS mytable("
                             "attribute CHAR NOT NULL PRIMARY KEY)");
    
        if(! success)
        {
            qDebug() << "SQL QUERY ERROR: " <<  query.lastError().text();
        }
    }
    
    MainWindow::~MainWindow()
    {
        delete ui;
    }
    
    
    void MainWindow::on_add_clicked()
    {
        if( ! ui->lineEdit->text().isEmpty())
        {
            QSqlQuery query;
    
            query.prepare("INSERT INTO mytable (attribute) VALUES(:attribute)");
    
            QByteArray obscuredStr = ui->lineEdit->text().toUtf8();
            obscuredStr = obscuredStr.toBase64();
            QString str = obscuredStr;
            qDebug() << str;
            query.bindValue(":attribute", str);
    
            if( ! query.exec())
            {
                qDebug() << "SQL QUERY ERROR: " + query.lastError().text();
            }
        }
    }
    
    
    void MainWindow::on_delete_2_clicked()
    {
        QSqlQuery query;
    
        QByteArray obscuredStr = ui->lineEdit->text().toUtf8();
        obscuredStr = obscuredStr.toBase64();
        qDebug() << obscuredStr;
    
        QString strt = obscuredStr;
        qDebug() << strt;
        if( ! query.exec("DELETE FROM mytable WHERE attribute = '" + strt + "'" ))
        {
            qDebug() << "SQL QUERY ERROR:" <<  query.lastError().text();
        }
    }
    

    .ui

    <?xml version="1.0" encoding="UTF-8"?>
    <ui version="4.0">
     <class>MainWindow</class>
     <widget class="QMainWindow" name="MainWindow">
      <property name="geometry">
       <rect>
        <x>0</x>
        <y>0</y>
        <width>305</width>
        <height>161</height>
       </rect>
      </property>
      <property name="windowTitle">
       <string>MainWindow</string>
      </property>
      <widget class="QWidget" name="centralwidget">
       <widget class="QPushButton" name="add">
        <property name="geometry">
         <rect>
          <x>10</x>
          <y>70</y>
          <width>121</width>
          <height>25</height>
         </rect>
        </property>
        <property name="text">
         <string>Add to table</string>
        </property>
       </widget>
       <widget class="QPushButton" name="delete_2">
        <property name="geometry">
         <rect>
          <x>150</x>
          <y>70</y>
          <width>121</width>
          <height>25</height>
         </rect>
        </property>
        <property name="text">
         <string>Delete from table</string>
        </property>
       </widget>
       <widget class="QLineEdit" name="lineEdit">
        <property name="geometry">
         <rect>
          <x>10</x>
          <y>40</y>
          <width>261</width>
          <height>24</height>
         </rect>
        </property>
       </widget>
      </widget>
      <widget class="QMenuBar" name="menubar">
       <property name="geometry">
        <rect>
         <x>0</x>
         <y>0</y>
         <width>305</width>
         <height>25</height>
        </rect>
       </property>
      </widget>
      <widget class="QStatusBar" name="statusbar"/>
     </widget>
     <resources/>
     <connections/>
    </ui>
    
    

    I also noticed from the example above that whenever I insert data into the table using a QByteArray object and then trying to delete the row of that data using the column that contains it nothing happens, no deletion and also and no error is thrown.

    Thank you all for helping me debug!!


Log in to reply