October 19, 2010

doforumda doforumda
Lab Rat
161 posts

help in this code for selecting data from db

 

hi now i am trying to display db data. the problem here is it is not displaying any error but it also does not display data from database.
here is my code
dvd_sw.h

  1. #ifndef DVD_SW_H
  2. #define DVD_SW_H
  3. #include "addDialog.h"
  4.  
  5. #include <QDialog>
  6. /*
  7. namespace Ui {
  8.     class dvd_sw;
  9. }
  10. */
  11. class QPushButton;
  12. class QLineEdit;
  13. class QTableView;
  14. class QLabel;
  15.  
  16. class dvd_sw : public QDialog
  17. {
  18.     Q_OBJECT
  19.  
  20. public:
  21.     dvd_sw(QWidget *parent = 0);
  22.     void select_dvds();
  23.     void connect_db();
  24.  
  25. public slots:
  26.     void add_dvd();
  27.  
  28. private:
  29.     QLabel *dvd_name_label;
  30.     QLabel *dvd_num_label;
  31.     QLineEdit *dvd_name_line;
  32.     QLineEdit *dvd_num_line;
  33.     QPushButton *add_button;
  34.     QTableView *data_table;
  35.     adddialog *dialog;
  36. };
  37.  
  38. #endif // DVD_SW_H

dvd_sw.cpp

  1. #include <QtGui>
  2. #include <QtSql>
  3. #include "dvd_sw.h"
  4. #include "ui_dvd_sw.h"
  5.  
  6. dvd_sw::dvd_sw(QWidget *parent) :
  7.     QDialog(parent)
  8. {
  9.      data_table = new QTableView;
  10.  
  11.      add_button = new QPushButton(tr("&Add DVD"));
  12.  
  13.      dialog = new adddialog;
  14.  
  15.      connect(add_button, SIGNAL(clicked()), this, SLOT(add_dvd()));
  16.  
  17.      QGridLayout *mainLayout = new QGridLayout;
  18.      mainLayout->addWidget(data_table, 0, 0);
  19.      mainLayout->addWidget(add_button, 1, 0);
  20.      setLayout(mainLayout);
  21.      setWindowTitle(tr("DVD Software"));
  22. }
  23.  
  24. void dvd_sw::add_dvd()
  25. {
  26.     dialog->show();
  27.  
  28.     if(dialog->exec() == QDialog::Accepted) {
  29.         QString getDvdName = dialog->nameOfDvd();
  30.     }
  31. }
  32.  
  33. void dvd_sw::select_dvds()
  34. {
  35.     connect_db();
  36.     QSqlTableModel *model = new QSqlTableModel;
  37.     model->setTable("dvdTable");
  38.     model->select();
  39.     model->setHeaderData(0, Qt::Horizontal, "First Name");
  40.     model->setHeaderData(1, Qt::Horizontal, "Last Name");
  41.  
  42.     //QTableView *view = new QTableView;
  43.     data_table->setModel(model);
  44.     data_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
  45. }
  46.  
  47. void dvd_sw::connect_db()
  48. {
  49.     QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
  50.     db.setHostName("localhost");
  51.     db.setDatabaseName("dvd");
  52.     db.setUserName("root");
  53.     db.setPassword("xxxxxx");
  54.  
  55.     bool ok = db.open();
  56.     if(ok) {
  57.         qDebug() << "Database opened";
  58.     }
  59.     else {
  60.         qDebug() << "Database not opened";
  61.     }
  62. }

main.cpp

  1. #include <QtGui/QApplication>
  2. #include "dvd_sw.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     dvd_sw w;
  8.     w.show();
  9.  
  10.     return a.exec();
  11. }

how can i solve this problem? help please

9 replies

October 19, 2010

Denis Kormalev Denis Kormalev
Lab Rat
1654 posts

Hm, looks like you never execute select_dvds() function that fetches and shows data.

October 19, 2010

doforumda doforumda
Lab Rat
161 posts

yep got it. newbie mistake :)

October 19, 2010

doforumda doforumda
Lab Rat
161 posts

now i changed my dvd_sw.cpp code and add insert query to this but it does not work.

  1. #include <QtGui>
  2. #include <QtSql>
  3. #include "dvd_sw.h"
  4. #include "ui_dvd_sw.h"
  5.  
  6. dvd_sw::dvd_sw(QWidget *parent) :
  7.     QDialog(parent)
  8. {
  9.      data_table = new QTableView;
  10.  
  11.      add_button = new QPushButton(tr("&Add DVD"));
  12.  
  13.      dialog = new adddialog;
  14.  
  15.      select_dvds();
  16.  
  17.      connect(add_button, SIGNAL(clicked()), this, SLOT(add_dvd()));
  18.  
  19.      QGridLayout *mainLayout = new QGridLayout;
  20.      mainLayout->addWidget(data_table, 0, 0);
  21.      mainLayout->addWidget(add_button, 1, 0);
  22.      setLayout(mainLayout);
  23.      setWindowTitle(tr("DVD Software"));
  24. }
  25.  
  26. void dvd_sw::add_dvd()
  27. {
  28.     dialog->show();
  29.  
  30.     if(dialog->exec() == QDialog::Accepted) {
  31.         QString getDvdName = dialog->nameOfDvd();
  32.  
  33.         connect_db();
  34.         QSqlQuery query;
  35.         bool q = query.exec("INSERT INTO dvdTable(id, dvdName) VALUES (NULL, getDvdName)");
  36.         if(q) {
  37.             QMessageBox::information(this, tr("Data Added"),
  38.                                      tr("\"%1\" has been added").arg(getDvdName));
  39.             select_dvds();
  40.         }
  41.         else {
  42.             QMessageBox::information(this, tr("Data add Failed"),
  43.                                      tr("\"%1\" could not be added").arg(getDvdName));
  44.         }
  45.     }
  46. }
  47.  
  48. void dvd_sw::select_dvds()
  49. {
  50.     connect_db();
  51.     QSqlTableModel *model = new QSqlTableModel;
  52.     model->setTable("dvdTable");
  53.     model->select();
  54.     model->setHeaderData(0, Qt::Horizontal, "First Name");
  55.     model->setHeaderData(1, Qt::Horizontal, "Last Name");
  56.  
  57.     //QTableView *view = new QTableView;
  58.     data_table->setModel(model);
  59.     data_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
  60. }
  61.  
  62. void dvd_sw::connect_db()
  63. {
  64.     QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
  65.     db.setHostName("localhost");
  66.     db.setDatabaseName("dvd");
  67.     db.setUserName("root");
  68.     db.setPassword("123456");
  69.  
  70.     bool ok = db.open();
  71.     if(ok) {
  72.         qDebug() << "Database opened";
  73.     }
  74.     else {
  75.         qDebug() << "Database not opened";
  76.     }
  77. }

October 19, 2010

ZapB ZapB
Robot Herder
1354 posts

It is because you are never actually calling your dvd_sw::select_dvds() function anywhere.

Change your main() function to:

  1. int main(int argc, char *argv[])
  2. {
  3.     QApplication a(argc, argv);
  4.     dvd_sw w;
  5.     w.select_dvds()
  6.     w.show();
  7.  
  8.     return a.exec();
  9. }

or simply call select_dvds() from your constructor.

Another problem is that you are also creating a model every time you can call the dvd_sw::select_dvds() function. Instead just add the model as a member variable and create it in the constructor along with the view.

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

October 19, 2010

doforumda doforumda
Lab Rat
161 posts

now i changed my code to something this which as follow. now it does not even display data from database.
dvd_sw.h

  1. #ifndef DVD_SW_H
  2. #define DVD_SW_H
  3. #include "addDialog.h"
  4.  
  5. #include <QDialog>
  6. #include <QtSql>
  7. /*
  8. namespace Ui {
  9.     class dvd_sw;
  10. }
  11. */
  12. class QPushButton;
  13. class QLineEdit;
  14. class QTableView;
  15. class QSqlTableModel;
  16. class QLabel;
  17.  
  18. class dvd_sw : public QDialog
  19. {
  20.     Q_OBJECT
  21.  
  22. public:
  23.     dvd_sw(QWidget *parent = 0);
  24.     void select_dvds();
  25.     void connect_db();
  26.  
  27. public slots:
  28.     void add_dvd();
  29.  
  30. private:
  31.     QLabel *dvd_name_label;
  32.     QLabel *dvd_num_label;
  33.     QLineEdit *dvd_name_line;
  34.     QLineEdit *dvd_num_line;
  35.     QPushButton *add_button;
  36.     QTableView *data_table;
  37.     QSqlTableModel *model;
  38.     adddialog *dialog;
  39. };
  40.  
  41. #endif // DVD_SW_H

dvd_sw.cpp

  1. #include <QtGui>
  2. //#include <QtSql>
  3. #include "dvd_sw.h"
  4. #include "ui_dvd_sw.h"
  5.  
  6. dvd_sw::dvd_sw(QWidget *parent) :
  7.     QDialog(parent)
  8. {
  9.      data_table = new QTableView;
  10.      model = new QSqlTableModel;
  11.  
  12.      add_button = new QPushButton(tr("&Add DVD"));
  13.  
  14.      dialog = new adddialog;
  15.  
  16.      //select_dvds();
  17.  
  18.      connect(add_button, SIGNAL(clicked()), this, SLOT(add_dvd()));
  19.  
  20.      QGridLayout *mainLayout = new QGridLayout;
  21.      mainLayout->addWidget(data_table, 0, 0);
  22.      mainLayout->addWidget(add_button, 1, 0);
  23.      setLayout(mainLayout);
  24.      setWindowTitle(tr("DVD Software"));
  25. }
  26.  
  27. void dvd_sw::add_dvd()
  28. {
  29.     dialog->show();
  30.  
  31.     if(dialog->exec() == QDialog::Accepted) {
  32.         QString getDvdName = dialog->nameOfDvd();
  33.  
  34.         connect_db();
  35.         QSqlQuery query;
  36.         bool q = query.exec("INSERT INTO dvdTable(id, dvdName) VALUES (NULL, getDvdName)");
  37.         if(q) {
  38.             QMessageBox::information(this, tr("Data Added"),
  39.                                      tr("\"%1\" has been added").arg(getDvdName));
  40.             //select_dvds();
  41.         }
  42.         else {
  43.             QMessageBox::information(this, tr("Data add Failed"),
  44.                                      tr("\"%1\" could not be added").arg(getDvdName));
  45.         }
  46.     }
  47. }
  48.  
  49. void dvd_sw::select_dvds()
  50. {
  51.     connect_db();
  52.     //QSqlTableModel *model = new QSqlTableModel;
  53.     model->setTable("dvdTable");
  54.     model->select();
  55.     model->setHeaderData(0, Qt::Horizontal, "ID");
  56.     model->setHeaderData(1, Qt::Horizontal, "DVD Name");
  57.  
  58.     //QTableView *view = new QTableView;
  59.     data_table->setModel(model);
  60.     data_table->setEditTriggers(QAbstractItemView::NoEditTriggers);
  61. }
  62.  
  63. void dvd_sw::connect_db()
  64. {
  65.     QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
  66.     db.setHostName("localhost");
  67.     db.setDatabaseName("dvd");
  68.     db.setUserName("root");
  69.     db.setPassword("xxxxxx");
  70.  
  71.     bool ok = db.open();
  72.     if(ok) {
  73.         qDebug() << "Database opened";
  74.     }
  75.     else {
  76.         qDebug() << "Database not opened";
  77.     }
  78. }

main.cpp

  1. #include <QtGui/QApplication>
  2. #include "dvd_sw.h"
  3.  
  4. int main(int argc, char *argv[])
  5. {
  6.     QApplication a(argc, argv);
  7.     dvd_sw w;
  8.     w.select_dvds();
  9.     w.show();
  10.  
  11.     return a.exec();
  12. }

October 19, 2010

ZapB ZapB
Robot Herder
1354 posts

Does your database actually contain any data? Are you sure you have the connection parameters correct? Is the table name correct? What is the output of running a simple “SELECT * FROM dvdTable” query?

Do you get any output form your application at all? Do you see the “Database opened” or “Database not opened” messages from connect_db()?

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

October 19, 2010

doforumda doforumda
Lab Rat
161 posts

well when i ran the code which is given at the start of this post it displayed data when i call select_db() function after line 15 which is now commented which is in database table.everything is correct here. it displays database opened whenever i execute this code. i dont know why it is not displaying data.

October 23, 2010

Eltharan Eltharan
Lab Rat
1 posts

Hi
I’ve got the same problem. In my case, model->select returns false. Db connection works, normal queries work, there is no database error after creating table. Code is almost copied from qtdoc and… it doesn’t work.

October 23, 2010

Volker Volker
Robot Herder
5428 posts

Does select() return true or false?
If true – the select was ok – can you get some values from the result via

  1. model.record(0).value("fieldname").toString();

If false – get the error via model->lastError().

Regarding the insert problem:
You must put the value of getDvdName into the query. In your version you try to put the literal string getDvdName as name into your database, which will fail, since it is not enclosed in single quotes (’).

It would work this way:

  1. QString sql = QString("INSERT INTO table (id, name) VALUES (NULL, '%1')").arg(getDvdName);

But this is suboptimal, since it allows for SQL injection. Better use:

  1. QString sql = "INSERT INTO table (id, name) VALUES (NULL, :DvdName)";
  2. QSqlQuery query;
  3. query.prepare(sql);
  4. query.bindValue(":DvdName", getDvdName);

See the docs of QSqlQuery [doc.trolltech.com] for further details.

 
  ‹‹ Is it possible to sort on multiple columns in QTableView using QSortFilterProxyModel      DLL problems ››

You must log in to post a reply. Not a member yet? Register here!