Hi,
I'm still new with QT and I have a simple application that add, search and remove records using SQLITE. But everytime I run the application my database is empty. Please check the partial code I'm using.
Qt Code:
  1. #include "phonebook.h"
  2. #include "ui_phonebook.h"
  3. #include <QMessageBox>
  4. //./phonebook.db
  5. PhoneBook::PhoneBook(QWidget *parent) :
  6. QWidget(parent),
  7. ui(new Ui::PhoneBook)
  8. {
  9. ui->setupUi(this);
  10.  
  11. database = new QSqlDatabase();
  12.  
  13. //set database driver to QSQLITE
  14. *database = QSqlDatabase::addDatabase("QSQLITE");
  15. database->setDatabaseName(":memory:");
  16. //database->setDatabaseName("./phonebook.db");
  17.  
  18. //can be removed
  19. database->setHostName("localhost");
  20. database->setUserName("");
  21. database->setPassword("");
  22.  
  23. if(!database->open())
  24. {
  25. QMessageBox::warning(0,"Error","Couldn't open database file.");
  26. }
  27.  
  28. QSqlQuery query;
  29. query.exec("CREATE TABLE IF NOT EXISTS Contacts (id int primary key, "
  30. "name varchar(20), mobile varchar(20),city varchar(20))");
  31. all_model = new QSqlTableModel(this, *database);
  32. updateTable();
  33.  
  34. search_model = new QSqlTableModel(this, *database);
  35. search_model->setTable("Contacts");
  36. }
To copy to clipboard, switch view to plain text mode 

regards,
lam-ang