Results 1 to 10 of 10

Thread: Sql query to retrieve database table data

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2013
    Posts
    71
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    7

    Default Re: Sql query to retrieve database table data

    Quote Originally Posted by Lesiok View Post
    In line 11 You define pointer ui. Next You use this pointer in line 13 and 14 but the pointer is NOT initialised.
    I suggest first learn the basics of C++.
    I saw this later on and have changed my code quite a bit since that post. At the moment, only 1 label and lineEdit is showing up with 1 record, how do I make it one for each record? And my question on how to loop through all the total number of names from the sql query?

    My code now:
    Qt Code:
    1.  
    2. if (qry.exec("SELECT name FROM customer"))
    3. {
    4. while(qry.next())
    5. {
    6. qDebug() << qry.value(0).toString();
    7. if(qry.isValid())
    8. {
    9. QString cust = qry.record().value(0).toString();
    10. QLabel *label = new QLabel(QString(cust));
    11. QLineEdit *lineEdit = new QLineEdit;
    12. lineEdit->setInputMask("0");
    13. lineEdit->setMaxLength(1);
    14. lineEdit->setGeometry(0,0,41,31);
    15. label->setGeometry(0,0,150,41);
    16.  
    17. ui->gridLayout->addWidget(label,0,0); //<<<<<<<<<<<Fills the entire width of layout,why?
    18. ui->gridLayout->addWidget(lineEdit,0,1);//<<<<<<<<<<<Fills the entire width of layout,why?
    19. }
    20. }
    21. }
    22. else
    23. {
    24. qDebug() << qry.lastError();
    25. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Cyrebo; 30th March 2013 at 08:35.

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: Sql query to retrieve database table data

    And what if the base will be a few thousand records ? Read about QSqlTableModel

  3. #3
    Join Date
    Feb 2013
    Posts
    71
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    7

    Default Re: Sql query to retrieve database table data

    I've managed to get it to read the names now and create a spinbox for each name rather than lineEdit/checkbox as previously. Now my problem is getting the value of the spinbox for each name like for example, the first name may get 1/2/3 value in the spinbox, how do i get this value and increment it in the database for that name? I don't know how to do this using QSqlTableModel for unknown names.

    Here is where I'm at:
    Qt Code:
    1.  
    2. if (qry.exec("SELECT name FROM customer"))
    3. {
    4. while(qry.next())
    5. {
    6. qDebug() << qry.value(0).toString();
    7. if(qry.isValid())
    8. {
    9. QString cust = qry.record().value(0).toString();
    10. QLabel *label = new QLabel(QString(cust));
    11. QSpinBox *spinbox = new QSpinBox;
    12. spinbox->setMaximum(3);
    13. label->setGeometry(0,0,80,41);
    14.  
    15. ui->verticalLayout->addWidget(label);
    16. ui->verticalLayout->addWidget(spinbox);
    17. }
    18. }
    19. }
    20. else
    21. {
    22. qDebug() << qry.lastError();
    23. }
    To copy to clipboard, switch view to plain text mode 
    Need to know how to get the spinbox value and increment it for the name beside the spinbox in the label. I'm thinking it requires some kind of pointer but not sure...

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,540
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanked 284 Times in 279 Posts

    Default Re: Sql query to retrieve database table data

    Tell us what your problem is rather than how You try to solve it.

  5. #5
    Join Date
    Feb 2013
    Posts
    71
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    7

    Default Re: Sql query to retrieve database table data

    Ok I want to read the value from the qspinbox I created specific to the label that the spin box that its beside. So something like name spinbox(1). I want to store that value to the person in the db table like on the form.

Similar Threads

  1. Replies: 3
    Last Post: 3rd October 2011, 08:08
  2. Replies: 1
    Last Post: 28th March 2010, 05:52
  3. SQL slow query - table update
    By lasher in forum Newbie
    Replies: 4
    Last Post: 21st October 2009, 23:12
  4. Replies: 2
    Last Post: 14th September 2009, 08:38
  5. Replies: 2
    Last Post: 14th September 2009, 08:31

Tags for this Thread

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
  •  
Qt is a trademark of The Qt Company.