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. Appcrash after sql statement with QString

Appcrash after sql statement with QString

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 852 Views
  • 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 Offline
    S Offline
    suggi87
    wrote on last edited by
    #1

    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
    0
    • C Offline
      C Offline
      clochydd
      wrote on last edited by
      #2

      Hi,

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

      1 Reply Last reply
      0
      • S Offline
        S Offline
        suggi87
        wrote on last edited by
        #3

        @
        "0588/835"
        @

        1 Reply Last reply
        0
        • C Offline
          C Offline
          clochydd
          wrote on last edited by
          #4

          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
          0
          • S Offline
            S Offline
            suggi87
            wrote on last edited by
            #5

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

            thanks anyway

            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