PDA

View Full Version : Sqlite Database



sabbu
5th May 2011, 14:55
i have a sqlite database i want to open and select one table from the database.and bind in combobox.but i have find error <unavailable synchroneous data>please help me i am sending the code.
////////////header///////////


#ifndef MYWIDGET_H
#define MYWIDGET_H

#include <QWidget>
#include<QtGui>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQueryModel>
#include<QtSql/QSqlTableModel>
#include<QtSql/QSqlRelationalTableModel>
#include<QtSql/QSqlRelation>

#include <QtCore>


namespace Ui {
class MyWidget;
}
class QListWidget;
class QListWidgetItem;
class QComboBox;
class QSqlRelationalTableModel;
class MyWidget : public QWidget
{
Q_OBJECT

public:
explicit MyWidget(QWidget *parent = 0);
~MyWidget();

private slots:
void itemClicked(QListWidgetItem *item);
bool createConnection() ;
private:

QListWidget *m_myListWidget;
QComboBox *typeComboBox;
QComboBox *typeComboBox2;
QLabel *nameLabel;
QLabel *addressLabel;
QLabel *typeLabel;
QLineEdit *nameEdit;
QPushButton *nextButton;

QVBoxLayout* lay;
QSizeGrip *gr ;//to resize the widget
QScrollArea *scroll;
QSqlDatabase db;
QSqlRelationalTableModel *model;
QItemSelectionModel *selectionModel;
QDataWidgetMapper *mapper;
int typeIndex;
};

#endif // MYWIDGET_H



////////////////////.cpp////////////


#include "mywidget.h"
#include "ui_mywidget.h"
#include <QtCore>
#include <QtSql/QSqlDatabase>

MyWidget::MyWidget(QWidget *parent) :
QWidget(parent)

{
m_myListWidget = new QListWidget(this);
new QListWidgetItem(tr("From"), m_myListWidget);

connect( m_myListWidget, SIGNAL(itemClicked(QListWidgetItem *)), SLOT(itemClicked (QListWidgetItem *)));

}
void MyWidget::itemClicked(QListWidgetItem *item)
{
m_myListWidget->hide();
createConnection() ;
model = new QSqlRelationalTableModel(this);
model->setTable("CityType");
model->setEditStrategy(QSqlTableModel::OnManualSubmit);
model->setRelation(typeIndex,QSqlRelation("CityType","rowid", "cityName"));
model->select();


addressLabel = new QLabel(tr("City:"));
typeComboBox = new QComboBox();
typeLabel = new QLabel(tr("&Area:"));
typeComboBox2 = new QComboBox();

nameLabel = new QLabel(tr("&street:"));
nameEdit = new QLineEdit();
nextButton = new QPushButton(tr("&Submit"));
// typeComboBox->addItem("Delhi");
// typeComboBox->addItem("Gurgaon");
// typeComboBox->addItem("Noida");
addressLabel->setBuddy(typeComboBox);
typeLabel->setBuddy(typeComboBox2);
nameLabel->setBuddy(nameEdit);

QSqlTableModel *relModel = model->relationModel(typeIndex);
typeComboBox->setModel(relModel);
typeComboBox->setModelColumn(relModel->fieldIndex("cityName"));
// typeComboBox->setModelColumn("cityName") ;



QGridLayout *layout = new QGridLayout();
layout->addWidget(addressLabel, 0, 0, 1, 1);
layout->addWidget(typeComboBox, 0, 1, 1, 1);
layout->addWidget(typeLabel, 1, 0, 1, 1);
layout->addWidget(typeComboBox2, 1, 1, 1, 1);
layout->addWidget( nameLabel, 2, 0, 1, 1);
layout->addWidget(nameEdit, 2, 1, 1, 1);
layout->addWidget(nextButton, 3, 1, 1, 1);
setLayout(layout);

item->~QListWidgetItem();




}
bool MyWidget::createConnection()
{
db = QSqlDatabase::addDatabase("QSQLITE");
//db .setDatabaseName("C:\NokiaQtSDK\project/MyWidget/CabBookingDatabase.Sqlite");
db.setDatabaseName("CabBookingDatabase.Sqlite");

if (!db.open())
{

return false;
}
return true;



}



MyWidget::~MyWidget()
{

}
///////////.pro///////////

QT += sql

amitkc
5th May 2011, 16:09
Can you test the value of typeIndex at the point you try and set the model on the combobox?

ChrisW67
6th May 2011, 05:17
Please use
tags around code.

Apart from the issue with the value of "typeIndex" it would seem that you probably have your relational table model quite wrong. You are trying to relate the base table CityType to itself saying that the cityName should be displayed in place of whatever column is "typeIndex". It seems unlikely that this is correct.

sabbu
7th May 2011, 12:31
Please use
tags around code.

Apart from the issue with the value of "typeIndex" it would seem that you probably have your relational table model quite wrong. You are trying to relate the base table CityType to itself saying that the cityName should be displayed in place of whatever column is "typeIndex". It seems unlikely that this is correct.

sorry ,i do another method but I have tried:
i have a table CityType and two column in table(rowid,cityName),i want to select cityName in combobox but i could not success please kindly see my code and where i am worng pls help me
//////////////////.header////////////////

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQueryModel>
#include<QtSql/QSqlTableModel>
#include<QtSql/QSqlRelationalTableModel>
#include<QtSql/QSqlRelation>
#include<QtSql/QSqlQuery>
#include <QtCore>
#include<QtSql/QSqlError>
namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
Q_OBJECT

public:
explicit MainWindow(QWidget *parent = 0);
~MainWindow();
private slots:

bool createConnection() ;
private:
Ui::MainWindow *ui;
QSqlDatabase db;
QSqlRelationalTableModel *model;
QSqlQuery query;
};

#endif // MAINWINDOW_H
////////////////////.cpp///////////////////


#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtCore>
#include<QtGui>
#include <QtSql/QSqlDatabase>
#include<QtSql/QSqlError>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
createConnection() ;
QSqlQuery query;
query.exec("SELECT cityName FROM CityType");

QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery( query);


QTableView *view = new QTableView;

ui->comboBox->setModel(model);
ui->comboBox->setView(view);

}
bool MainWindow:: createConnection()
{
db = QSqlDatabase::addDatabase("QSQLITE");
QSqlDatabase::addDatabase("C:/NokiaQtSDK/project/untitled1","CabBookingDatabase.Sqlite");
db.setDatabaseName("CabBookingDatabase.Sqlite");
if (!db.open()) {
QMessageBox::critical(0, QObject::tr("Database Error"),
db.lastError().text());
return false;
}
return true;

}
MainWindow::~MainWindow()
{
delete ui;
}
///////////////////////////man.cpp//////

#include <QtGui/QApplication>
#include "mainwindow.h"

int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();

return a.exec();
}

ChrisW67
8th May 2011, 08:18
Please use
tags around code. Edit your post to fix it. Until we have line numbers on your code it is hard to reference the errors.

The combobox drop-down works here (after I turned your code into something that could be built). How about describing the problem rather than just saying "it doesn't work" and throwing your code at us? What behaviour are you expecting? What behaviour are you getting? What have you tried to fix the problem?

Here are some broad hints about your code:

createConnection() contains rubbish statements that are generating warnings you are probably ignoring.
createConnection() is a private slot?
createConnection returns a value that is routinely ignored.
You don't need a custom view for a single column in a combo box.

None of these things stop it working.

sabbu
16th May 2011, 13:07
thank , sir i getting items in combox