Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Installation and Deployment
  4. [Solved] Qt problem with query in SQLite database in Qt

[Solved] Qt problem with query in SQLite database in Qt

Scheduled Pinned Locked Moved Installation and Deployment
5 Posts 2 Posters 1.4k Views 1 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.
  • M Offline
    M Offline
    maroko
    wrote on last edited by
    #1

    Hi,
    I am facing this problem:

    @QString name, passwd;
    name=ui->nom->text();
    passwd=ui->motdepasse->text();
    if(!mydb.isOpen())
    {
    ui->label_2->setText("pas de connexion");
    //qDebug()<<"Failed to open my database";
    //return;
    }
    QSqlQuery qry;
    if(qry.exec("SELECT * FROM emploi WHERE ref='"+passwd+"' AND entreprise='"+name+"'"))
    {
    int count=0;
    while(qry.next())
    {
    count++;
    }
    if(count==1)

           ui->label_2->setText("username and passwd are correct");
    
        else if(count>1)
    
           ui->label_2->setText("duplication");
    
        else
    
           ui->label_2->setText("this count does not exist");
    
    
    }
    else ui->label_2->setText("errors");
    

    }
    @
    When running I get the message "errors" that mean the query exec return false.
    Also when I replace the previous query with @ if(qry.exec("SELECT * FROM emploi WHERE ref=0 AND entreprise='alcatel'"))@
    the program run correctly and poster this message "username and passwd are correct"(I have it in my table emploi).
    Can someone help me and thank you.

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

      Hi,
      I suggest to use qry.bindValue instead of putting your variables directly into QString, "see here: ":http://qt-project.org/doc/qt-5/qsqlquery.html#bindValue-2
      Your problem seems to be related to the field "ref" which seems to be integer and you want to pass a string-value.

      1 Reply Last reply
      0
      • M Offline
        M Offline
        maroko
        wrote on last edited by
        #3

        Yes ref in an integer but even with varchar it does not work .

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

          You may try this:

          @
          ...
          QSqlQuery qry;
          qry.prepare("SELECT * FROM emploi WHERE ref = :pref AND entreprise= :pent;")
          qry.bindValue(":pref", passwd);
          qry.bindValue(":pent", name);

          if (!qry.exec()) {
          qDebug() << qry.lastError(); // to see what's the problem
          } else {
          int count=0;
          while(qry.next())
          ...
          }
          @

          1 Reply Last reply
          0
          • M Offline
            M Offline
            maroko
            wrote on last edited by
            #5

            Thank you very much for your help.Now it works correctly.

            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