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
- #ifndef DVD_SW_H
- #define DVD_SW_H
- #include "addDialog.h"
- #include <QDialog>
- /*
- namespace Ui {
- class dvd_sw;
- }
- */
- class QPushButton;
- class QLineEdit;
- class QTableView;
- class QLabel;
- {
- Q_OBJECT
- public:
- void select_dvds();
- void connect_db();
- public slots:
- void add_dvd();
- private:
- adddialog *dialog;
- };
- #endif // DVD_SW_H
dvd_sw.cpp
- #include <QtGui>
- #include <QtSql>
- #include "dvd_sw.h"
- #include "ui_dvd_sw.h"
- {
- data_table = new QTableView;
- dialog = new adddialog;
- connect(add_button, SIGNAL(clicked()), this, SLOT(add_dvd()));
- mainLayout->addWidget(data_table, 0, 0);
- mainLayout->addWidget(add_button, 1, 0);
- setLayout(mainLayout);
- setWindowTitle(tr("DVD Software"));
- }
- void dvd_sw::add_dvd()
- {
- dialog->show();
- }
- }
- void dvd_sw::select_dvds()
- {
- connect_db();
- model->setTable("dvdTable");
- model->select();
- //QTableView *view = new QTableView;
- data_table->setModel(model);
- }
- void dvd_sw::connect_db()
- {
- db.setHostName("localhost");
- db.setDatabaseName("dvd");
- db.setUserName("root");
- db.setPassword("xxxxxx");
- bool ok = db.open();
- if(ok) {
- qDebug() << "Database opened";
- }
- else {
- qDebug() << "Database not opened";
- }
- }
main.cpp
- #include <QtGui/QApplication>
- #include "dvd_sw.h"
- int main(int argc, char *argv[])
- {
- dvd_sw w;
- w.show();
- return a.exec();
- }
how can i solve this problem? help please
9 replies
now i changed my dvd_sw.cpp code and add insert query to this but it does not work.
- #include <QtGui>
- #include <QtSql>
- #include "dvd_sw.h"
- #include "ui_dvd_sw.h"
- {
- data_table = new QTableView;
- dialog = new adddialog;
- select_dvds();
- connect(add_button, SIGNAL(clicked()), this, SLOT(add_dvd()));
- mainLayout->addWidget(data_table, 0, 0);
- mainLayout->addWidget(add_button, 1, 0);
- setLayout(mainLayout);
- setWindowTitle(tr("DVD Software"));
- }
- void dvd_sw::add_dvd()
- {
- dialog->show();
- connect_db();
- QSqlQuery query;
- bool q = query.exec("INSERT INTO dvdTable(id, dvdName) VALUES (NULL, getDvdName)");
- if(q) {
- tr("\"%1\" has been added").arg(getDvdName));
- select_dvds();
- }
- else {
- tr("\"%1\" could not be added").arg(getDvdName));
- }
- }
- }
- void dvd_sw::select_dvds()
- {
- connect_db();
- model->setTable("dvdTable");
- model->select();
- //QTableView *view = new QTableView;
- data_table->setModel(model);
- }
- void dvd_sw::connect_db()
- {
- db.setHostName("localhost");
- db.setDatabaseName("dvd");
- db.setUserName("root");
- db.setPassword("123456");
- bool ok = db.open();
- if(ok) {
- qDebug() << "Database opened";
- }
- else {
- qDebug() << "Database not opened";
- }
- }
It is because you are never actually calling your dvd_sw::select_dvds() function anywhere.
Change your main() function to:
- int main(int argc, char *argv[])
- {
- dvd_sw w;
- w.select_dvds()
- w.show();
- return a.exec();
- }
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.
now i changed my code to something this which as follow. now it does not even display data from database.
dvd_sw.h
- #ifndef DVD_SW_H
- #define DVD_SW_H
- #include "addDialog.h"
- #include <QDialog>
- #include <QtSql>
- /*
- namespace Ui {
- class dvd_sw;
- }
- */
- class QPushButton;
- class QLineEdit;
- class QTableView;
- class QSqlTableModel;
- class QLabel;
- {
- Q_OBJECT
- public:
- void select_dvds();
- void connect_db();
- public slots:
- void add_dvd();
- private:
- adddialog *dialog;
- };
- #endif // DVD_SW_H
dvd_sw.cpp
- #include <QtGui>
- //#include <QtSql>
- #include "dvd_sw.h"
- #include "ui_dvd_sw.h"
- {
- data_table = new QTableView;
- model = new QSqlTableModel;
- dialog = new adddialog;
- //select_dvds();
- connect(add_button, SIGNAL(clicked()), this, SLOT(add_dvd()));
- mainLayout->addWidget(data_table, 0, 0);
- mainLayout->addWidget(add_button, 1, 0);
- setLayout(mainLayout);
- setWindowTitle(tr("DVD Software"));
- }
- void dvd_sw::add_dvd()
- {
- dialog->show();
- connect_db();
- QSqlQuery query;
- bool q = query.exec("INSERT INTO dvdTable(id, dvdName) VALUES (NULL, getDvdName)");
- if(q) {
- tr("\"%1\" has been added").arg(getDvdName));
- //select_dvds();
- }
- else {
- tr("\"%1\" could not be added").arg(getDvdName));
- }
- }
- }
- void dvd_sw::select_dvds()
- {
- connect_db();
- //QSqlTableModel *model = new QSqlTableModel;
- model->setTable("dvdTable");
- model->select();
- //QTableView *view = new QTableView;
- data_table->setModel(model);
- }
- void dvd_sw::connect_db()
- {
- db.setHostName("localhost");
- db.setDatabaseName("dvd");
- db.setUserName("root");
- db.setPassword("xxxxxx");
- bool ok = db.open();
- if(ok) {
- qDebug() << "Database opened";
- }
- else {
- qDebug() << "Database not opened";
- }
- }
main.cpp
- #include <QtGui/QApplication>
- #include "dvd_sw.h"
- int main(int argc, char *argv[])
- {
- dvd_sw w;
- w.select_dvds();
- w.show();
- return a.exec();
- }
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()?
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.
Does select() return true or false?
If true – the select was ok – can you get some values from the result via
- 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:
But this is suboptimal, since it allows for SQL injection. Better use:
- QSqlQuery query;
- query.prepare(sql);
- query.bindValue(":DvdName", getDvdName);
See the docs of QSqlQuery [doc.trolltech.com] for further details.
You must log in to post a reply. Not a member yet? Register here!



