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(
....
if (createConnection())
{
initializeModel();
createView();
....
}
}
mainWindow::manWindow(
....
db = QSqlDatabase::addDatabase("QSQLITE");
if (createConnection())
{
initializeModel();
createView();
updateDetails(QModelIndex());
....
}
}
To copy to clipboard, switch view to plain text mode
and
bool mainWindow::createConnection()
{
/* If the database do not exist
I create one anew*/
check.setFileName(databaseName);
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;
}
bool mainWindow::createConnection()
{
/* If the database do not exist
I create one anew*/
QFile check;
check.setFileName(databaseName);
QDir::setCurrent(QCoreApplication::applicationDirPath());
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;
}
To copy to clipboard, switch view to plain text mode
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)"));
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();
statusLabel->setText(currentDatabase);
}
}
}
currentDatabase = databaseName;
return true;
}
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;
}
To copy to clipboard, switch view to plain text mode
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
Bookmarks