Results 1 to 10 of 10

Thread: Sql query to retrieve database table data

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

    Default Sql query to retrieve database table data

    Hi, how do I retrieve certain content like name from a table? I want to retrieve all the names and then set them to a label in my form. For now I just want to how to get all the names. So far I've tried using a select count query to first get the number of rows but it didn't work. I'm not sure what functions to use for this if anyone can please help.

    Here's what I have done:
    Qt Code:
    1. db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName(Path_to_DB);
    3. QFileInfo checkFile(Path_to_DB);
    4.  
    5. if(checkFile.isFile())
    6. {
    7. if(db.open())
    8. {
    9. qDebug() << "Connected to database file";
    10. }
    11. }else{
    12. qDebug() << "Database file not found";
    13. }
    14.  
    15. QSqlQuery query;
    16. QString qry = QString("SELECT Count(*) FROM customers");
    17.  
    18. query.prepare(qry);
    19.  
    20. if (query.exec())
    21. {
    22. int count = query.result(); //<<<Where I'm stuck
    23. qDebug() << count;
    24. for ( int i = 0; i < count; i++)
    25. {
    26. //create labels for each customer
    27. QLabel *label = new QLabel(QString());
    28. QCheckBox *chkbox = new QCheckBox;
    29.  
    30. Ui::MainWindow *ui;
    31.  
    32. ui->gridLayout->addWidget(label,0,0);
    33. ui->gridLayout->addWidget(chkbox,0,1);
    34.  
    35. }
    36. }
    37. else
    38. {
    39. query.lastError();
    40. }
    To copy to clipboard, switch view to plain text mode 

    Thanks in advance!

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Sql query to retrieve database table data

    Hi,

    see QSqlQuery::value(). Further there is no need to query the total size first. Just do
    sql Code:
    1. SELECT name FROM customers
    To copy to clipboard, switch view to plain text mode 
    and then loop through it using QSqlQuery::next(). This way you can replace your current for loop.


    EDIT: And here you do not need prepare!

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

    Default Re: Sql query to retrieve database table data

    Thanks for the quick response, the sql is sort of working because this will not actually let me implement the second part of my code..as you can see the last half of my code is commented out because my program flat out crashes when its not. I get the dreaded "The program has finished unexpectedly error" that gives no feedback on what caused it to so I'm not sure what I'm doing wrong here...

    How would I implement a loop to create a label and checkbox for each item in the db individually?

    Qt Code:
    1.  
    2. if (qry.exec("SELECT name FROM customers"))
    3. {
    4. while(qry.next())
    5. {
    6. qDebug() << qry.value(0).toString();
    7. // QLabel *label = new QLabel(QString());
    8. // QCheckBox *chkbox = new QCheckBox;
    9.  
    10. // Ui::MainWindow *ui;
    11.  
    12. // ui->gridLayout->addWidget(label,0,0);
    13. // ui->gridLayout->addWidget(chkbox,0,1);
    14. }
    15. }
    16. else
    17. {
    18. qDebug() << qry.lastError();
    19. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Cyrebo; 29th March 2013 at 23:28.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Sql query to retrieve database table data

    Uhh, exactly like you have, except you would not try to put all of the labels into the same cell of the grid layout.

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

    Default Re: Sql query to retrieve database table data

    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++.

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

    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.

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

    Default Re: Sql query to retrieve database table data

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

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

    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...

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

    Default Re: Sql query to retrieve database table data

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

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

    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.