Results 1 to 6 of 6

Thread: Sqlite Database

  1. #1
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Post Sqlite Database

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

    Qt Code:
    1. #ifndef MYWIDGET_H
    2. #define MYWIDGET_H
    3.  
    4. #include <QWidget>
    5. #include<QtGui>
    6. #include <QtSql/QSqlDatabase>
    7. #include <QtSql/QSqlQueryModel>
    8. #include<QtSql/QSqlTableModel>
    9. #include<QtSql/QSqlRelationalTableModel>
    10. #include<QtSql/QSqlRelation>
    11.  
    12. #include <QtCore>
    13.  
    14.  
    15. namespace Ui {
    16. class MyWidget;
    17. }
    18. class QComboBox;
    19. class MyWidget : public QWidget
    20. {
    21. Q_OBJECT
    22.  
    23. public:
    24. explicit MyWidget(QWidget *parent = 0);
    25. ~MyWidget();
    26.  
    27. private slots:
    28. void itemClicked(QListWidgetItem *item);
    29. bool createConnection() ;
    30. private:
    31.  
    32. QListWidget *m_myListWidget;
    33. QComboBox *typeComboBox;
    34. QComboBox *typeComboBox2;
    35. QLabel *nameLabel;
    36. QLabel *addressLabel;
    37. QLabel *typeLabel;
    38. QLineEdit *nameEdit;
    39. QPushButton *nextButton;
    40.  
    41. QSizeGrip *gr ;//to resize the widget
    42. QScrollArea *scroll;
    43. QItemSelectionModel *selectionModel;
    44. int typeIndex;
    45. };
    46.  
    47. #endif // MYWIDGET_H
    To copy to clipboard, switch view to plain text mode 



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

    Qt Code:
    1. #include "mywidget.h"
    2. #include "ui_mywidget.h"
    3. #include <QtCore>
    4. #include <QtSql/QSqlDatabase>
    5.  
    6. MyWidget::MyWidget(QWidget *parent) :
    7. QWidget(parent)
    8.  
    9. {
    10. m_myListWidget = new QListWidget(this);
    11. new QListWidgetItem(tr("From"), m_myListWidget);
    12.  
    13. connect( m_myListWidget, SIGNAL(itemClicked(QListWidgetItem *)), SLOT(itemClicked (QListWidgetItem *)));
    14.  
    15. }
    16. void MyWidget::itemClicked(QListWidgetItem *item)
    17. {
    18. m_myListWidget->hide();
    19. createConnection() ;
    20. model = new QSqlRelationalTableModel(this);
    21. model->setTable("CityType");
    22. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    23. model->setRelation(typeIndex,QSqlRelation("CityType","rowid", "cityName"));
    24. model->select();
    25.  
    26.  
    27. addressLabel = new QLabel(tr("City:"));
    28. typeComboBox = new QComboBox();
    29. typeLabel = new QLabel(tr("&Area:"));
    30. typeComboBox2 = new QComboBox();
    31.  
    32. nameLabel = new QLabel(tr("&street:"));
    33. nameEdit = new QLineEdit();
    34. nextButton = new QPushButton(tr("&Submit"));
    35. // typeComboBox->addItem("Delhi");
    36. // typeComboBox->addItem("Gurgaon");
    37. // typeComboBox->addItem("Noida");
    38. addressLabel->setBuddy(typeComboBox);
    39. typeLabel->setBuddy(typeComboBox2);
    40. nameLabel->setBuddy(nameEdit);
    41.  
    42. QSqlTableModel *relModel = model->relationModel(typeIndex);
    43. typeComboBox->setModel(relModel);
    44. typeComboBox->setModelColumn(relModel->fieldIndex("cityName"));
    45. // typeComboBox->setModelColumn("cityName") ;
    46.  
    47.  
    48.  
    49. QGridLayout *layout = new QGridLayout();
    50. layout->addWidget(addressLabel, 0, 0, 1, 1);
    51. layout->addWidget(typeComboBox, 0, 1, 1, 1);
    52. layout->addWidget(typeLabel, 1, 0, 1, 1);
    53. layout->addWidget(typeComboBox2, 1, 1, 1, 1);
    54. layout->addWidget( nameLabel, 2, 0, 1, 1);
    55. layout->addWidget(nameEdit, 2, 1, 1, 1);
    56. layout->addWidget(nextButton, 3, 1, 1, 1);
    57. setLayout(layout);
    58.  
    59. item->~QListWidgetItem();
    60.  
    61.  
    62.  
    63.  
    64. }
    65. bool MyWidget::createConnection()
    66. {
    67. db = QSqlDatabase::addDatabase("QSQLITE");
    68. //db .setDatabaseName("C:\NokiaQtSDK\project/MyWidget/CabBookingDatabase.Sqlite");
    69. db.setDatabaseName("CabBookingDatabase.Sqlite");
    70.  
    71. if (!db.open())
    72. {
    73.  
    74. return false;
    75. }
    76. return true;
    77.  
    78.  
    79.  
    80. }
    81.  
    82.  
    83.  
    84. MyWidget::~MyWidget()
    85. {
    86.  
    87. }
    To copy to clipboard, switch view to plain text mode 
    ///////////.pro///////////

    QT += sql
    Last edited by Lykurg; 8th May 2011 at 09:19. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2007
    Location
    Hampton, Middlesex, UK
    Posts
    6
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Sqlite Database

    Can you test the value of typeIndex at the point you try and set the model on the combobox?

  3. #3
    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: Sqlite Database

    Please use [code][/code] 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.

  4. #4
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default Re: Sqlite Database

    Quote Originally Posted by ChrisW67 View Post
    Please use [code][/code] 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////////////////
    Qt Code:
    1. #ifndef MAINWINDOW_H
    2. #define MAINWINDOW_H
    3.  
    4. #include <QMainWindow>
    5. #include <QtSql/QSqlDatabase>
    6. #include <QtSql/QSqlQueryModel>
    7. #include<QtSql/QSqlTableModel>
    8. #include<QtSql/QSqlRelationalTableModel>
    9. #include<QtSql/QSqlRelation>
    10. #include<QtSql/QSqlQuery>
    11. #include <QtCore>
    12. #include<QtSql/QSqlError>
    13. namespace Ui {
    14. class MainWindow;
    15. }
    16.  
    17. class MainWindow : public QMainWindow
    18. {
    19. Q_OBJECT
    20.  
    21. public:
    22. explicit MainWindow(QWidget *parent = 0);
    23. ~MainWindow();
    24. private slots:
    25.  
    26. bool createConnection() ;
    27. private:
    28. Ui::MainWindow *ui;
    29. QSqlQuery query;
    30. };
    31.  
    32. #endif // MAINWINDOW_H
    To copy to clipboard, switch view to plain text mode 
    ////////////////////.cpp///////////////////

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtCore>
    4. #include<QtGui>
    5. #include <QtSql/QSqlDatabase>
    6. #include<QtSql/QSqlError>
    7. MainWindow::MainWindow(QWidget *parent) :
    8. QMainWindow(parent),
    9. ui(new Ui::MainWindow)
    10. {
    11. ui->setupUi(this);
    12. createConnection() ;
    13. QSqlQuery query;
    14. query.exec("SELECT cityName FROM CityType");
    15.  
    16. model->setQuery( query);
    17.  
    18.  
    19. QTableView *view = new QTableView;
    20.  
    21. ui->comboBox->setModel(model);
    22. ui->comboBox->setView(view);
    23.  
    24. }
    25. bool MainWindow:: createConnection()
    26. {
    27. db = QSqlDatabase::addDatabase("QSQLITE");
    28. QSqlDatabase::addDatabase("C:/NokiaQtSDK/project/untitled1","CabBookingDatabase.Sqlite");
    29. db.setDatabaseName("CabBookingDatabase.Sqlite");
    30. if (!db.open()) {
    31. QMessageBox::critical(0, QObject::tr("Database Error"),
    32. db.lastError().text());
    33. return false;
    34. }
    35. return true;
    36.  
    37. }
    38. MainWindow::~MainWindow()
    39. {
    40. delete ui;
    41. }
    To copy to clipboard, switch view to plain text mode 
    ///////////////////////////man.cpp//////
    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include "mainwindow.h"
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6. QApplication a(argc, argv);
    7. MainWindow w;
    8. w.show();
    9.  
    10. return a.exec();
    11. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by Lykurg; 8th May 2011 at 09:20. Reason: missing [code] tags

  5. #5
    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: Sqlite Database

    Please use [code][/code] 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.

  6. #6
    Join Date
    Apr 2011
    Posts
    27
    Qt products
    Qt3 Qt4
    Platforms
    Symbian S60

    Default Re: Sqlite Database

    thank , sir i getting items in combox

Similar Threads

  1. sqlite database problem
    By palinko1111 in forum Qt Programming
    Replies: 3
    Last Post: 4th May 2011, 07:04
  2. How to insert row to SQLite database?
    By MIH1406 in forum Qt Programming
    Replies: 6
    Last Post: 29th May 2010, 12:22
  3. SQLITE ATTACH database
    By drescherjm in forum Qt Programming
    Replies: 8
    Last Post: 9th December 2009, 07:25
  4. SQLITE database problems
    By phoenix in forum Newbie
    Replies: 3
    Last Post: 30th April 2007, 21:38
  5. [QT4][SQLITE] Database and query
    By agent007se in forum Newbie
    Replies: 10
    Last Post: 12th July 2006, 22:16

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.