Hi, I tried it and unfortunately it does nos compile...
#include "phonebook.h"
#include "ui_phonebook.h"
#include <QMessageBox>
#include <QDesktopservices.h>
//./phonebook.db
PhoneBook
::PhoneBook(QWidget *parent
) : ui(new Ui::PhoneBook)
{
ui->setupUi(this);
//set database driver to QSQLITE
// database->setDatabaseName(":memory:");
//database->setDatabaseName("./phonebook.db");
database->setDatabaseName(dbName);
//can be removed
//database->setHostName("localhost");
//database->setUserName("");
//database->setPassword("");
if(!database->open()) {
QMessageBox::warning(0,
"Error",tr
("Couldn't open database file: %1").
arg(database
->lastError
().
text()));
}
query.exec("CREATE TABLE IF NOT EXISTS Contacts (id int primary key, "
"name varchar(20), mobile varchar(20),city varchar(20))");
updateTable();
search_model->setTable("Contacts");
}
#include "phonebook.h"
#include "ui_phonebook.h"
#include <QMessageBox>
#include <QDesktopservices.h>
//./phonebook.db
PhoneBook::PhoneBook(QWidget *parent) :
QWidget(parent),
ui(new Ui::PhoneBook)
{
ui->setupUi(this);
database = new QSqlDatabase();
//set database driver to QSQLITE
*database = QSqlDatabase::addDatabase("QSQLITE");
// database->setDatabaseName(":memory:");
//database->setDatabaseName("./phonebook.db");
QString dbName = QDesktopServices::storageLocation (QDesktopServices::DataLocation) + "/phonebook.db";
database->setDatabaseName(dbName);
//can be removed
//database->setHostName("localhost");
//database->setUserName("");
//database->setPassword("");
if(!database->open()) {
QMessageBox::warning(0,"Error",tr("Couldn't open database file: %1").arg(database->lastError().text()));
}
QSqlQuery query;
query.exec("CREATE TABLE IF NOT EXISTS Contacts (id int primary key, "
"name varchar(20), mobile varchar(20),city varchar(20))");
all_model = new QSqlTableModel(this, *database);
updateTable();
search_model = new QSqlTableModel(this, *database);
search_model->setTable("Contacts");
}
To copy to clipboard, switch view to plain text mode
But if I comment this line like this..
if(!database->open()) {
QMessageBox::warning(0,
"Error",tr
("Couldn't open database file: %1"));
//.arg(database->lastError().text())); }
if(!database->open()) {
QMessageBox::warning(0,"Error",tr("Couldn't open database file: %1"));//.arg(database->lastError().text()));
}
To copy to clipboard, switch view to plain text mode
and here is the result when compiled...
Executable file: 15482 2011-06-13T15:12:58 C:\QtSDK\Symbian\SDKs\Symbian1Qt473\\epoc32\release\gcce\urel\SQLite_example.exe
Starting application...
Application running with pid 4355.
[Qt Message
] QSqlQuery::exec: database not open
Executable file: 15482 2011-06-13T15:12:58 C:\QtSDK\Symbian\SDKs\Symbian1Qt473\\epoc32\release\gcce\urel\SQLite_example.exe
Starting application...
Application running with pid 4355.
[Qt Message] QSqlQuery::exec: database not open
To copy to clipboard, switch view to plain text mode
thanks,
lam-ang
Bookmarks