Appcrash after sql statement with QString



  • Hello,

    If i execute this sql-statement my app crashes if I change a ComboBox item

    @
    void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
    {

    if(!db.isOpen())
    {
        qDebug() << "DB is not open!";
    }
    
    QSqlQuery q;
    q.exec&#40;"SELECT * FROM autos WHERE snr = '"+ arg1 +"'"&#41;;
    
    
    }
    

    @

    If i use this it works...

    @
    void MainWindow::on_comboBox_currentIndexChanged(const QString &arg1)
    {

    if(!db.isOpen())
    {
        qDebug() << "DB is not open!";
    }
    
    QSqlQuery q;
    q.exec&#40;"SELECT * FROM autos WHERE snr = '0588/835'"&#41;;
    
    
    }
    

    @

    why....???? :/



  • Hi,

    what's the content of arg1?
    @
    qDebug() << arg1;
    @



  • @
    "0588/835"
    @



  • Try to bind the value for snr:
    @
    QSqlQuery q;
    q.prepare("SELECT * FROM autos WHERE snr = :sachnr");
    q.bindValue(":sachnr", arg1);
    if (!q.exec()) {
    qDebug() << q.lastError().text()
    return;
    } else {
    //...
    }
    @



  • I don´t know why, but now both solutions are working...

    thanks anyway


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.