When I want to open new connection for SQLite I faced with strange problem!!
-
When I want to connect to a DB with the connection name according to the source code In debug output I am faced with some strange error..!!!
And all query not works. Without a connection name, in the QSqlDatabase::addDatabase method, I had not any problem.
How can open a new connection with a connection name?QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "connection"); QString dbName = QDir::currentPath() + "/" + "scheduleddb"; db.setDatabaseName(dbName); if (!db.open()) { qDebug() << "not connected to db: " << db.database() << " ."; } else { qDebug() << " connected to db: " << db.database() << " ."; }Eror:
Connected to db: QSqlDatabase(invalid) . QSqlQuery::exec: database not open -
When I want to connect to a DB with the connection name according to the source code In debug output I am faced with some strange error..!!!
And all query not works. Without a connection name, in the QSqlDatabase::addDatabase method, I had not any problem.
How can open a new connection with a connection name?QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "connection"); QString dbName = QDir::currentPath() + "/" + "scheduleddb"; db.setDatabaseName(dbName); if (!db.open()) { qDebug() << "not connected to db: " << db.database() << " ."; } else { qDebug() << " connected to db: " << db.database() << " ."; }Eror:
Connected to db: QSqlDatabase(invalid) . QSqlQuery::exec: database not open@stackprogramer said in When I want to open new connection for SQLite I faced with strange problem!!:
QSqlQuery::exec: database not open
This is not in your code above so please post the real code. Als QDir::currentPath() looks fishy - this directory can change e.g. during a call to QFileDialog (native mode) or others - don't use it for such stuff.
-
QSqlDatabase _db;
QString currentPath=QDir::currentPath();
QString dbPath=currentPath+"/files/"+Database;//where Database is the name of you db for example : TestDB.db;QFile file(dbPath); if(file.exists()) { if (!QSqlDatabase::contains(Database)) { _db = QSqlDatabase::addDatabase("QSQLITE",Database); _db.setDatabaseName(dbPath); } } else{ emit losingConnection_Signal(); return false; }_db.open();
-
QSqlDatabase _db;
QString currentPath=QDir::currentPath();
QString dbPath=currentPath+"/files/"+Database;//where Database is the name of you db for example : TestDB.db;QFile file(dbPath); if(file.exists()) { if (!QSqlDatabase::contains(Database)) { _db = QSqlDatabase::addDatabase("QSQLITE",Database); _db.setDatabaseName(dbPath); } } else{ emit losingConnection_Signal(); return false; }_db.open();
This post is deleted! -
QSqlDatabase _db;
QString currentPath=QDir::currentPath();
QString dbPath=currentPath+"/files/"+Database;//where Database is the name of you db for example : TestDB.db;QFile file(dbPath); if(file.exists()) { if (!QSqlDatabase::contains(Database)) { _db = QSqlDatabase::addDatabase("QSQLITE",Database); _db.setDatabaseName(dbPath); } } else{ emit losingConnection_Signal(); return false; }_db.open();
-
This post is deleted!
-
QSqlDatabase _db;
QString currentPath=QDir::currentPath();
QString dbPath=currentPath+"/files/"+Database;//where Database is the name of you db for example : TestDB.db;QFile file(dbPath); if(file.exists()) { if (!QSqlDatabase::contains(Database)) { _db = QSqlDatabase::addDatabase("QSQLITE",Database); _db.setDatabaseName(dbPath); } } else{ emit losingConnection_Signal(); return false; }_db.open();
@m-hosseini Thanks very much. my problem is solved.