Results 1 to 6 of 6

Thread: Qt SQLite same name different surname lineEdit

  1. #1
    Join Date
    Feb 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Qt SQLite same name different surname lineEdit

    Qt Code:
    1. #include "widget.h"
    2. #include "ui_widget.h"
    3.  
    4. Widget::Widget(QWidget *parent) :
    5. QWidget(parent),
    6. ui(new Ui::Widget)
    7. {
    8. ui->setupUi(this);
    9.  
    10. mydb = QSqlDatabase::addDatabase("QSQLITE");
    11. mydb.setDatabaseName("C:/sqlite/etudiant.sqlite");
    12.  
    13. if(!mydb.open())
    14. {
    15. qDebug() << "Fail";
    16. }
    17.  
    18. else
    19. {
    20.  
    21. qDebug() << "connected";
    22.  
    23. QSqlQuery *req = new QSqlQuery(mydb);
    24. req->prepare("SELECT name FROM note");
    25.  
    26. req->exec();
    27. modal->setQuery(*req);
    28. ui->comboBox->setModel(modal);
    29. }
    30. }
    31.  
    32. Widget::~Widget()
    33. {
    34. delete ui;
    35. }
    36.  
    37. void Widget::on_comboBox_currentIndexChanged(const QString &arg1)
    38. {
    39. QString name = ui->comboBox->currentText();
    40.  
    41. QSqlQuery qry;
    42. qry.prepare("SELECT * FROM note WHERE name = '"+name+"'");
    43.  
    44. if(qry.exec())
    45. {
    46. while (qry.next()) {
    47. ui->lineEdit->setText(qry.value(1).toString());
    48. ui->lineEdit_2->setText(qry.value(2).toString());
    49. }
    50. }
    51.  
    52. }
    53.  
    54. CREATE TABLE note(id INTEGER PRIMARY KEY, name VARCHAR(25), surname VARCHAR(25));
    55. INSERT INTO note VALUES(1, 'Jhon', 'Samuel');INSERT INTO note VALUES(2, 'Eddy', 'Ron');INSERT INTO note VALUES(3, 'Jhon', 'Mark');
    To copy to clipboard, switch view to plain text mode 


    hello everybody , got a small problem with a tutorial was following on youtube, 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:

    6796579801.jpg
    2365202802.jpg
    4030187203.jpg

    hope you can gimmy some help to continu my learnings in the right way, thx , I knew by some researches that my code will only call to display last 'Jhon' he got everytime, but how can I correct my code? some code lines will help a lot, thx again !!

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt SQLite same name different surname lineEdit

    Quote Originally Posted by Rastate View Post
    hope you can gimmy some help to continu my learnings in the right way, thx , I knew by some researches that my code will only call to display last 'Jhon' he got everytime, but how can I correct my code? some code lines will help a lot, thx again !!
    You are interating over the result set, which contains more than one entry.
    The while loop ends when it has the processed the last entry, which is your second Jhon.

    The example simulates a search, if you want to access the data of a specific entry, address that entry directly or get a list of all entries and used the index to address the one you want.

    Btw:
    - you are leaking the query in the constructor, just pass the SQL string to the model
    - your search query uses string concatenation instead of placeholders, this is what gets software hacked

    Cheers,
    _

  3. #3
    Join Date
    Feb 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qt SQLite same name different surname lineEdit

    uhm? and how can I resolve that (even if partially) :'|?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt SQLite same name different surname lineEdit

    Quote Originally Posted by Rastate View Post
    uhm? and how can I resolve that (even if partially) :'|?
    Resolve what?
    Not leaking the query -> already told you.
    Using place holders -> QSqlQuery::prepare().
    Finding the full data of the respective entry -> already suggested two options. E.g. your table has an id column. If that starts with ß, then you could use the currentIndexChanged signal that has an integer argument and use that to query.

    Cheers,
    _

  5. #5
    Join Date
    Feb 2016
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows Android

    Default Re: Qt SQLite same name different surname lineEdit

    I'm a real begginer, can you provide me some code line please?

  6. #6
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Qt SQLite same name different surname lineEdit

    Assuming the table's id starts at 0 like the combobox's index:
    Qt Code:
    1. qry.prepare("SELECT * FROM note WHERE id = :id");
    2. qry.bindValue(":id", index);
    To copy to clipboard, switch view to plain text mode 

    Cheers,
    _

Similar Threads

  1. lineedit
    By madhukumar in forum Qt Programming
    Replies: 1
    Last Post: 17th February 2014, 19:17
  2. LineEdit's
    By Johnnyj2j in forum Newbie
    Replies: 6
    Last Post: 9th July 2012, 12:34
  3. Spellchecking in lineedit
    By mkas in forum Newbie
    Replies: 0
    Last Post: 27th August 2010, 11:54
  4. LineEdit.
    By Rewo in forum Newbie
    Replies: 17
    Last Post: 2nd July 2010, 09:37
  5. [Qt][SQLite] Two problems with SQLite.
    By Xandareva in forum Newbie
    Replies: 6
    Last Post: 6th April 2010, 23:06

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.