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. Qt SqLite Same Name Different Surname
Forum Updated to NodeBB v4.3 + New Features

Qt SqLite Same Name Different Surname

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 2 Posters 1.3k 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.
  • R Offline
    R Offline
    Rastate
    wrote on 25 Feb 2016, 19:56 last edited by
    #1
    #include "widget.h"
    #include "ui_widget.h"
    
    Widget::Widget(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::Widget)
    {
        ui->setupUi(this);
    
        mydb = QSqlDatabase::addDatabase("QSQLITE");
        mydb.setDatabaseName("C:/sqlite/etudiant.sqlite");
    
        if(!mydb.open())
        {
            qDebug() << "Fail";
        }
    
        else
        {
            QSqlQueryModel *modal = new QSqlQueryModel();
    
            qDebug() << "connected";
    
            QSqlQuery *req = new QSqlQuery(mydb);
            req->prepare("SELECT name FROM note");
    
            req->exec();
            modal->setQuery(*req);
                ui->comboBox->setModel(modal);
        }
    }
    
    Widget::~Widget()
    {
        delete ui;
    }
    
    void Widget::on_comboBox_currentIndexChanged(const QString &arg1)
    {
        QString name = ui->comboBox->currentText();
    
        QSqlQuery qry;
        qry.prepare("SELECT * FROM note WHERE name = '"+name+"'");
    
        if(qry.exec())
        {
            while (qry.next()) {
                ui->lineEdit->setText(qry.value(1).toString());
                ui->lineEdit_2->setText(qry.value(2).toString());
            }
        }
    
    }
    CREATE TABLE note(id INTEGER PRIMARY KEY, name VARCHAR(25), surname VARCHAR(25));
    
    INSERT INTO note VALUES(1, 'Jhon', 'Samuel');
    INSERT INTO note VALUES(2, 'Eddy', 'Ron');
    INSERT INTO note VALUES(3, 'Jhon', 'Mark');
    

    well, hello all :p, got a little problem with a tutorial was following, where when the program couldn't display ONLY last name+surname in LineEdit, even when I select the first one in comboBox, just like that:
    image1
    image2
    image3
    hope I'll get some help :p to continu my learnings in the good way, thx :) !!

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 25 Feb 2016, 20:48 last edited by
      #2

      Hi and welcome to devnet,

      Samuel and Mark have the same name, so you'll get two entries with your query. Since you're looping over the result and overwrite the content of lineEdit and lineEdit2, you will only see the value of the last element returned by your query.

      On a side note, why are you creating your model's query on the heap ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • R Offline
        R Offline
        Rastate
        wrote on 25 Feb 2016, 22:29 last edited by
        #3

        it's all time 'Jhon' who's asked to display, and with the the while loop it displays only last 'Jhon' all time, but how could I correct that? with a "currentIndexChanged(int index)" ? how can I use it please :)?

        1 Reply Last reply
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 25 Feb 2016, 22:41 last edited by
          #4

          Don't use a query then. You already have everything at hand through your QComboBox model just modify the original query of the model to retrieve both the name and surname. Your combo box will only show one column.

          And then in your function, retrieve the surname again with the model.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • R Offline
            R Offline
            Rastate
            wrote on 25 Feb 2016, 23:06 last edited by
            #5

            can I use both name AND surname on the comboBox?(if I understood what you was talkin' about :')!) and how can I retrieve the surname with the model? some code lines would help me a lot :p

            1 Reply Last reply
            0
            • S Offline
              S Offline
              SGaist
              Lifetime Qt Champion
              wrote on 26 Feb 2016, 12:47 last edited by
              #6

              Yes you can, just change your SQL query to concatenate both.
              Since you are using sqlite it should be something like:

              SELECT name || ' ' || surname as fullname FROM note

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              0

              1/6

              25 Feb 2016, 19:56

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved