Results 1 to 6 of 6

Thread: Sqlite Database

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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.