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.2k 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.
  • Christian EhrlicherC Christian Ehrlicher

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

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

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

    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