Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Appcrash after sql statement with QString

    General and Desktop
    2
    5
    708
    Loading More Posts
    • 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.
    • S
      suggi87 last edited by

      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....???? :/

      1 Reply Last reply Reply Quote 0
      • C
        clochydd last edited by

        Hi,

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

        1 Reply Last reply Reply Quote 0
        • S
          suggi87 last edited by

          @
          "0588/835"
          @

          1 Reply Last reply Reply Quote 0
          • C
            clochydd last edited by

            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 {
            //...
            }
            @

            1 Reply Last reply Reply Quote 0
            • S
              suggi87 last edited by

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

              thanks anyway

              1 Reply Last reply Reply Quote 0
              • First post
                Last post