I have two function, one is called at the start time, and the other is called whenever i want to create a new database.
bool mainWindow::createConnection()
{
/* If the database do not exist
I create one anew*/
check.setFileName(databaseName);
#ifdef DEBUG
qDebug()<<QCoreApplication::applicationDirPath();
#endif
db.setDatabaseName(databaseName);
if (!check.exists()){
QMessageBox::warning(0,
QObject::tr("Database mancante"),
"Sembra che il database precedente sia stato cancellato. Ne ho creato uno nuovo con lo stesso nome");
#ifdef DEBUG
qDebug()<<databaseName<<db.open();
#endif
createTable();
}
if (!db.open()) {
QMessageBox::warning(0,
QObject::tr("Non riesco a connettermi al database"),
"Controlla che un databse sia presente o scegline uno dal menu File");
#ifdef DEBUG
qDebug()<<db.lastError()<<db.open();
#endif
return false;
}
return true;
}
bool mainWindow::createConnection()
{
db = QSqlDatabase::addDatabase("QSQLITE");
/* If the database do not exist
I create one anew*/
QFile check;
check.setFileName(databaseName);
QDir::setCurrent(QCoreApplication::applicationDirPath());
#ifdef DEBUG
qDebug()<<QCoreApplication::applicationDirPath();
#endif
db.setDatabaseName(databaseName);
if (!check.exists()){
QMessageBox::warning(0,QObject::tr("Database mancante"),"Sembra che il database precedente sia stato cancellato. Ne ho creato uno nuovo con lo stesso nome");
#ifdef DEBUG
qDebug()<<databaseName<<db.open();
#endif
createTable();
}
if (!db.open()) {
QMessageBox::warning(0,QObject::tr("Non riesco a connettermi al database"),"Controlla che un databse sia presente o scegline uno dal menu File");
#ifdef DEBUG
qDebug()<<db.lastError()<<db.open();
#endif
return false;
}
return true;
}
To copy to clipboard, switch view to plain text mode
void mainWindow::newDB()
{
/* Creat un nuovo archivio*/
if (db.isOpen())
{
removeConnection();
#ifdef DEBUG
qDebug()<<"newDB, closing conn.:"<<db.isOpen();
#endif
}
QString fileName
= QFileDialog::getSaveFileName(this, tr
("Nuovo archivio"),
QDir::homePath (),tr
("Rubic (*.db)"));
db.setDatabaseName(fileName);
#ifdef DEBUG
qDebug()<<"newDB, creating"<<db.lastError()<<db.isOpen()<<fileName<<db.isValid();
#endif
if (db.isValid()){
createTable();
databaseName=fileName;
}
}
void mainWindow::newDB()
{
/* Creat un nuovo archivio*/
if (db.isOpen())
{
removeConnection();
#ifdef DEBUG
qDebug()<<"newDB, closing conn.:"<<db.isOpen();
#endif
}
QString fileName = QFileDialog::getSaveFileName(this, tr("Nuovo archivio"),QDir::homePath (),tr("Rubic (*.db)"));
db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(fileName);
#ifdef DEBUG
qDebug()<<"newDB, creating"<<db.lastError()<<db.isOpen()<<fileName<<db.isValid();
#endif
if (db.isValid()){
createTable();
databaseName=fileName;
}
}
To copy to clipboard, switch view to plain text mode
But the debug says to me that, when I call newDB(), removeConnection() and db = QSqlDatabase::addDatabase("QSQLITE");, there is still in use the qt_sql_default connection.
The function removeConnection() is
void mainWindow::removeConnection()
{
// db.close();
}
void mainWindow::removeConnection()
{
// db.close();
QSqlDatabase::removeDatabase(db.connectionName());
}
To copy to clipboard, switch view to plain text mode
For this reason, I cannot start a new database.
Help appreciated.
Bookmarks