View Full Version : Need help on QSQLITE
lam-ang
10th June 2011, 00:57
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.
#include "phonebook.h"
#include "ui_phonebook.h"
#include <QMessageBox>
//./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");
//can be removed
database->setHostName("localhost");
database->setUserName("");
database->setPassword("");
if(!database->open())
{
QMessageBox::warning(0,"Error","Couldn't open database file.");
}
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");
}
regards,
lam-ang
mcosta
10th June 2011, 10:37
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.
The problem is at line 15
database->setDatabaseName(":memory:");
you're using a "in memory" SQLITE database.
Comment this line and uncomment the line 16
database->setDatabaseName("./phonebook.db");
lam-ang
10th June 2011, 11:40
Hi mcosta,
I appreciate the help and the time...I did what you suggested but this give me the output.
Couldn't open database file.
is this line means it's creating a database name phonebook or is it only identifying the location of the file?
database->setDatabaseName("./phonebook.db");
regards,
lam-ang
mcosta
10th June 2011, 14:58
When the database does'n exist, open create it.
Probably you don't have permission to create file in the directory where the program are installed.
Try using your DATA directory.
Use QDesktopServices; for example
QString dbName = QDesktopServices::storageLocation (QDesktopServices::DataLocation) + "/phonebook.db";
database->setDatabaseName(dbName);
lam-ang
11th June 2011, 15:45
Hi,
I tried what you suggested and I added QDesktopService header but I got the same result "Couldn't open database file".
regards,
lam-ang
mcosta
11th June 2011, 16:38
Print the Database Driver Message Please
if(!database->open()) {
QMessageBox::warning(0,"Error",tr("Couldn't open database file: %1").arg(database.lasteError().text()));
}
lam-ang
11th June 2011, 20:01
Print the Database Driver Message Please
if(!database->open()) {
QMessageBox::warning(0,"Error",tr("Couldn't open database file: %1").arg(database.lasteError().text()));
}
Hi,
Thanks for the help and the time, but it seems I'm having trouble compiling.
C:\Users\Severpc\Documents\QtFiles1.1\SQLite_examp le-build-simulator\..\SQLite_example\phonebook.cpp:29: error: request for member 'lasteError' in '((PhoneBook*)this)->PhoneBook::database', which is of non-class type 'QSqlDatabase*'
regards,
picuser
Lykurg
12th June 2011, 08:38
It's a typo. It has to be lastError().
mcosta
12th June 2011, 10:53
It's a typo. It has to be lastError().
:o I'm sorry!!
lam-ang
12th June 2011, 12:35
Hi, I still have errors and would not compile..
:-1: warning: The Symbian tool chain does not handle special characters in the project name 'SQLite_example.pro' well.
I noticed if I remove this part of code ".arg(database.lasteError().text())" it compiles.
regards,
lam-ang
mcosta
13th June 2011, 06:22
The correct code is
if(!database->open()) {
QMessageBox::warning(0,"Error",tr("Couldn't open database file: %1").arg(database->lastError().text()));
}
try it please
lam-ang
13th June 2011, 08:10
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) :
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");
}
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()));
}
and here is the result when compiled...
Executable file: 15482 2011-06-13T15:12:58 C:\QtSDK\Symbian\SDKs\Symbian1Qt473\\epoc32\releas e\gcce\urel\SQLite_example.exe
Starting application...
Application running with pid 4355.
[Qt Message] QSqlQuery::exec: database not open
thanks,
lam-ang
mcosta
13th June 2011, 14:02
Hi, I tried it and unfortunately it does nos compile...
What is the error?
Probably you have to add some include in your source. Perhaps
#include <QtSql/QSqlError>
vishnu
13th June 2011, 14:46
Hi
It seems you can run execute your application successfully. But DB is empty/volatile if you restart your qt application. am I right?
if yes,
Try this
Qt Code:
database->setDatabaseName("memory.db");
and use CREATE TABLE command rather than CREATE TABLE IF NOT EXISTS Contacts.
Thanks,
Vishnu
lam-ang
13th June 2011, 15:54
Hello there,
Thanks for helping me out, I appreciate it very much...its running fine now.
I hope it is okay if I have another question, how can I use another database like Mysql if my target is a pc..any idea or link?
thanks and regards,
lam-ang
vishnu
22nd June 2011, 11:07
Hello,
IF you want to know about the SQL support drivers,
use the following debug code,
debugLog("Driver supports name "<<db.drivers());
you can get the list supporting drivers name.
I think PC will support the following the drivers ("QSQLITE", "QPSQL7", "QPSQL") by default
Powered by vBulletin® Version 4.2.5 Copyright © 2024 vBulletin Solutions Inc. All rights reserved.