[SOLVED] Qt Creator in Kubuntu QDatabase - QSQLITE - driver not found
-
wrote on 22 Jul 2014, 16:43 last edited by
[../../../electricity/src/AtpCore/AtpDb.cpp:49, static bool AtpDb::setNewConnection()]
Critical: couldn't connect to database Error[ "Driver not loaded Driver not loaded" ] "/media/andrei/Munca/atp-trading/qt-electricity/electricity/dbfile/electricityy.atpdb"
[kernel/qsqlquery.cpp:968, bool QSqlQuery::prepare(const QString&):]
Warning: QSqlQuery::prepare: database not opendoes anyone know why is not loading the driver?
@QSqlDatabase *myNewdb = new QSqlDatabase();
if (!QSqlDatabase::isDriverAvailable(dbNewType)){
return false;
}
myNewdb->addDatabase(dbNewType);
qDebug() << dbNewType;
myNewdb->setDatabaseName(dbNewPath);
if (!myNewdb->open()){
qCritical() << "couldn't connect to database Error[" << myNewdb->lastError().text() << "]" << dbPath;
return false;
} else {
qDebug() << "succsessfully connected to database " << dbPath;
}@
the value of dbNewType is "QSQLITE"
this is the list ov available drivers printed with:
@qDebug() << QSqlDatabase::drivers();
Debug: ("QSQLITE", "QMYSQL", "QMYSQL3", "QPSQL", "QPSQL7") @
kindly help me to get out of this.. lost few hoour only today.. hm... frustratd because of it...
is simple but I can not see where is it.. -
wrote on 22 Jul 2014, 17:04 last edited by
I think you are using addDatabase wrong. It's a static function so you should use like this.
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
You should check QSqlDatabase documentation.
"QSqlDatabase":http://qt-project.org/doc/qt-5/QSqlDatabase.html -
wrote on 22 Jul 2014, 17:24 last edited by
ok... but as yu can see I've used to initialize the database
@QSqlDatabase *myNewdb = new QSqlDatabase();@
so now I can not use
@QSqlDatabase db = QSqlDatabase::addDatabase(“QSQLITE”);@ -
wrote on 22 Jul 2014, 17:39 last edited by
@QSqlDatabase *myNewdb = new QSqlDatabase(QSqlDatabase::addDatabase(“QSQLITE”));@
This should work.
-
wrote on 22 Jul 2014, 18:00 last edited by
unfortunatele I have to accept that this will work like that but I do not understand why
@myNewdb->setDatabaseName(dbNewPath);@
is provided and is not working??
any way now I've changed the constructor and is fine
@
QSqlDatabase myNewdb;
if (!QSqlDatabase::isDriverAvailable(dbNewType)){
return false;
}
myNewdb = QSqlDatabase::addDatabase(dbNewType);
qDebug() << dbNewType;
myNewdb.setDatabaseName(dbNewPath);
if (!myNewdb.open()){
qCritical() << "couldn't connect to database Error[" << myNewdb.lastError().text() << "]" << dbPath;
return false;
} else {
qDebug() << "succsessfully connected to database " << dbPath;
}
@but that is not nice at all for what i have to create... any way..... I've worked around and is working almost fine
-
wrote on 22 Jul 2014, 18:03 last edited by
[quote author="Mr. Puskevit" date="1406050768"]@QSqlDatabase *myNewdb = new QSqlDatabase(QSqlDatabase::addDatabase(“QSQLITE”));@
This should work.
[/quote]I will try this later on.. now I will take a break.. see you at around 11 or 12 in this evening...
Until then.. have a great joy programing.. and debugging - this is most time consumming for me :) -
Don't use a pointer to a QSqlDatabase, there's no need for that. It's all well explained in the documentation.
-
wrote on 22 Jul 2014, 23:37 last edited by
well any way the pointer is not working.. but for what I'm doing I will need a pointer.... and now just for ypu to get an Ideea why I will need a pointer here you have...
- AtpDb.cpp
http://pastebin.com/JK5enqeB
*AtpDb.h
http://pastebin.com/eQ2Xpec4my wish is to access the class just from any side of the program without any need to do all the the checks and not to init anything just by
@AtpDb::Select(select statement...)@
and each have it's oun return that is convenient for me...
plus I can unini with @AtpDb::uninit;@ and I'm avoiding SIGSEGV witch I wish to avoid.. so basicly because of SIGSEGV I will need this to be a pointer.. any help for creating a singleton? - AtpDb.cpp
-
Writing a wrapper class is fine, but I don't see the point of inheriting QSqlDataBase, you are not extending it, you are just using it.
Again I encourage you to re-read the documentation of QSqlDataBase, you don't need to keep a pointer to the current database connection. You can retrieve it when needed and from your code it doesn't look like you have to. QSqlQuery uses the default connection automatically so you don't have to pass anything special. And in the case you would, you can retrieve the correct connection with QSqlDatabase::database("my_other_connection")
-
wrote on 24 Jul 2014, 08:19 last edited by
yep, this is true, but what I've been thinking to archive is to be able to manage the connection. user is able too change the database file in the settings and if the user change the database file, the file can be wrong so I have to reset the connection and to know if is a valid connection and to destroy the old connection and to be only one connection at any time so like a singleton. Because I wish to pass as the argument to QSqlQuery the connection and to check is the connection is available... I'm afraid that will never happen for the user to change the database file or to create a new one and the class not to be destroyed and recreated...
but yes I've read many times the QSqlDatabase so I've just changed my code and at the moment it doesn't extend anything. is just a pure class. I will see the behaviour.. but now When I have already a connection and create a new one.. I do not know why I receive the SIGSEGV on the cloase of the program (mainWindow). witch is not received if I do n ot create a new connection and stick with the one created at the beginning.. I will debug it.. any way.. many thanks for your help.. highly apreciated.
2/10