PDA

View Full Version : Sqlite data base



sabbu
19th May 2011, 13:02
sir i have a database i want to take city name in combobox when i select city then according to city all sector in combobox .please help me my code is this
/////header//////////////
#ifndef WINDOW_H
#define WINDOW_H

#include <QWidget>

QT_BEGIN_NAMESPACE
class QComboBox;
class QDataWidgetMapper;
class QItemSelectionModel;
class QLabel;
class QSqlRelationalTableModel;
class QStandardItemModel;
class QStringListModel;
class QTextEdit;
class QSqlTableModel;
QT_END_NAMESPACE

//! [Window definition]
class Window : public QWidget
{
Q_OBJECT

public:
Window(QWidget *parent = 0);

private slots:


private:
void setupModel();

QLabel *nameLabel;
QLabel *typeLabel;
QComboBox *cityComboBox;
QComboBox *typeComboBox;
QSqlRelationalTableModel *model;
QItemSelectionModel *selectionModel;
QDataWidgetMapper *mapper;
QSqlTableModel *relModel;
QSqlTableModel *re;
int index;
int typeIndex;
};
//! [Window definition]

#endif
/////////////////////.cpp//////////////////
#include <QtGui>
#include <QtSql>

#include "window.h"

//! [Set up widgets]
Window::Window(QWidget *parent)
: QWidget(parent)
{
setupModel();

nameLabel = new QLabel(tr("Na&me:"));
cityComboBox = new QComboBox();
typeLabel = new QLabel(tr("&Type:"));
typeComboBox = new QComboBox();


nameLabel->setBuddy( cityComboBox);
typeLabel->setBuddy(typeComboBox);
//! [Set up widgets]

//! [Set up the mapper]
re=new QSqlTableModel ();
re=model->relationModel(index);
cityComboBox->setModel(re );
cityComboBox->setModelColumn(re->fieldIndex("name"));
relModel=new QSqlTableModel() ;
relModel = model->relationModel(typeIndex);
typeComboBox->setModel(relModel);
typeComboBox->setModelColumn(relModel->fieldIndex("area"));

mapper = new QDataWidgetMapper(this);
mapper->setModel(model);
mapper->setItemDelegate(new QSqlRelationalDelegate(this));
//mapper->addMapping( areaComboBox, model->fieldIndex("name"));
// mapper->addMapping(addressEdit, model->fieldIndex("address"));
mapper->addMapping(cityComboBox,index);
mapper->addMapping(typeComboBox, typeIndex);




//! [Set up the mapper]

//! [Set up connections and layouts]


QGridLayout *layout = new QGridLayout();
layout->addWidget(nameLabel, 0, 0, 1, 1);
layout->addWidget( cityComboBox, 0, 1, 1, 1);
layout->addWidget(typeLabel,1, 0, 1, 1);
layout->addWidget(typeComboBox, 1, 1, 1, 1);
setLayout(layout);



}
//! [Set up connections and layouts]

//! [Set up the main table]
void Window::setupModel()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open()) {
QMessageBox::critical(0, tr("Cannot open database"),
tr("Unable to establish a database connection.\n"
"This example needs SQLite support. Please read "
"the Qt SQL driver documentation for information how "
"to build it."), QMessageBox::Cancel);
return;
}

QSqlQuery query;
query.exec("create table person (Id int primary key,name varchar(20), typeid int)");
query.exec("insert into person values(1, 'Delhi',101)");
query.exec("insert into person values(2, 'Gurgaon',102)");
query.exec("insert into person values(3, 'Noida',103)");
query.exec("insert into person values(4, 'Gaziabad',104)");
query.exec("insert into person values(5, 'faridabad',105)");
//! [Set up the main table]

//! [Set up the address type table]
query.exec("create table addresstype (id int, area varchar(20))");
query.exec("insert into addresstype values(101, 'susantlok')");
query.exec("insert into addresstype values(101, 'dlf phase2')");
query.exec("insert into addresstype values(101, 'phase1')");

model = new QSqlRelationalTableModel(this);
model->setTable("person");


index=model->fieldIndex("Id");
model->setRelation(index,QSqlRelation("person", "Id", "name"));

typeIndex = model->fieldIndex("typeid");
model->setRelation(typeIndex,QSqlRelation("addresstype", "id", "area"));
model->select();
}
please help me i am tired

schnitzel
19th May 2011, 17:22
Generally, people are not going to help you if you provide some code and 'here you go'.

some tips:

state the problem clearly
show what you have already tried
use code tags so your source code is more readable
check out some of the Qt examples

ChrisW67
19th May 2011, 23:43
Generally, people are not going to help you if if you provide some code and 'here you go'
... or ask a series of the same question in various guises:
http://www.qtcentre.org/threads/41707-Sqlite-data-base?p=190898&highlight=#post190898
http://www.qtcentre.org/threads/41246-how-to-bind-Sqlite-database-with-combobox?p=188944&highlight=#post188944
http://www.qtcentre.org/threads/41300-Sqlite-Database?p=189242&highlight=#post189242

and largely ignore the responses.

The forum participants do not exist to do your learning for you, or to do your paying work for you. Both of those things are up to you.

Added after 16 minutes:

To help you help yourself, try to correct this summary of your question.

This is the problem:

You have a list of cities
You have a related list of sectors for each city
You have a form with two combo boxes
One combo box should display the list of cities and allow selection of one.
When you select a city in the combo box, you want the other combo box to show the sectors related to that city. When no city is selected the other combo box should be empty.