PDA

View Full Version : Sqlite3 window problem



giusepped
6th November 2008, 15:23
There are differences between QSQLITE in windows and in linux?
In linux, if the database doesnot exist Qt create a new file with the databasename.
In windows it does not, although I can use the database.
Perhaps, the sqlite database has been creating in memory, or what?
G

spirit
6th November 2008, 15:49
on windows Qt also creates file with databasename in app directory.
but if you this like this


QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");

then the database will be allocated in memory.

giusepped
6th November 2008, 15:56
I did not.





QSqlDatabase d = QSqlDatabase::database();
d.setDatabaseName(databaseName);


Still, I cannot see the file in the any folder, but I can do sql query!
How is it possible?
even if I close the application the database file is not present....
:confused:

spirit
6th November 2008, 16:03
did you change work directory (dest dir) in pro-file?

giusepped
6th November 2008, 16:05
I don't know what you are talking about....
Please, shine me!

spirit
6th November 2008, 16:08
ok, build an troll's example. go to QTDIR/examples/sql and in connection.h change the next lines


// db.setDatabaseName(":memory:");
db.setDatabaseName("test.db");

then go (for example) to cachedtable dir, build and run this example. the "test.db" file should be with executable file.

giusepped
6th November 2008, 16:15
I still do not understand.
My exec is in debug folder.
If I try to create a database e.g. d.setDatabaseName("pinco.db"), the database FILE is correctly created in the debug folder.
BUT: If i try to create d.setDatabaseName("C:/Docuemnts and Settings/giuse") the file IS NOT CREATED, but I can make queries!
G

spirit
6th November 2008, 16:23
I've tried to connect to a database with name wich you specified and a file was created.

giusepped
6th November 2008, 16:26
You are under windows?
The same code under linux works (the file is created). But in windows not.
I am desperate.
Anyway, a solution is to close the connection, reopen it with addDatabase.
In this case the file is created.
G

spirit
6th November 2008, 16:29
I'm under windos now and a file creates fine. which version of Qt do you use? maybe this is a bug. I use 4.3.5.

giusepped
7th November 2008, 01:49
I did not want to post the code, but I am really confused.
Actually, the error is present in linux too.
In my mainwindow constructor I did



mainWindow::manWindow(
....
db = QSqlDatabase::addDatabase("QSQLITE");
if (createConnection())
{
initializeModel();
createView();
updateDetails(QModelIndex());
....
}
}

and


bool mainWindow::createConnection()
{


/* If the database do not exist
I create one anew*/

QFile check;
check.setFileName(databaseName);
QDir::setCurrent(QCoreApplication::applicationDirP ath());
db.setDatabaseName(databaseName);

if (!check.exists()){

#ifdef DEBUG
qDebug()<<databaseName<<"do not exist. However, is open="<<db.open();
#endif
currentDatabase = databaseName ;
createTable();
statusLabel->setText(currentDatabase);
}
if (!db.open()) {

#ifdef DEBUG
qDebug()<<db.lastError()<<db.open();
#endif
removeConnection();
return false;
}


When I wanna add a new datatabase (which in SQLITE means a new file!) I do



void mainWindow::newDB()
{
/* Create a new archive*/

QString fileName = QFileDialog::getSaveFileName(this, tr("Nuovo archivio"),QDir::homePath (),tr("Rubic database (*.db)"));
QFile f(fileName);


if (fileName != currentDatabase)
{
db.setDatabaseName(fileName);
#ifdef DEBUG
qDebug()<<"newDB, opening"<<db.lastError()<<"is open="<<db.isOpen()<<fileName<<db.isValid();
#endif
if (db.isValid()){
currentDatabase = fileName;
createTable();
initializeModel();
createView();
updateDetails(QModelIndex());
statusLabel->setText(currentDatabase);
}
}
}

currentDatabase = databaseName;
return true;

}

The funny think is
1) After the start up, If I create a newDB, the debug messages say that the database has been created. IN fact I can use the queries but I cannot see any database file in the choosen dir
2) If I quit the application, since a write the currentDatabase var. inside a QSettings, the debu message says that the database does not exist and it will create it.
3) The only way I found to let it work is to addDatabase also in the newDB function.

Any suggestion appreciated

giusepped
7th November 2008, 02:18
I found a solution but I do not understand why it is working.
First I create a static member function in mainWindow:



bool mainWindow::createConnStatic()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");

}

which is declared static in the header file.

Now, at the startup I create a connection by calling this function. db is a member variable.
When I wann change database, I do


QSqlDatabase db = QSqlDatabase::database();
db.setDatabaseName(s);

This work pretty well, but is "impossible" to got there by reading the Qt Doc.