[solved] Application crash QSqlQuery(dbconnection) is called.
-
At first sight...
In
database.h
you don't includeQSqlDatabase
or have a forward declaration for it. I'm going to assume you've addedQT += sql
in your.pro
for it to compile . Usually if you press F5 QtCreator will show you the lines where it stopped at. (not a silver bullet, but at the very least it will give you an idea. (it can be messy).). -
Hi,
Are you sure you are not accessing an uninitialized variable ? You use
model
in several function as local variable, does it shadow a class member ? -
Is dbCon correctly configured before calling open ? What does lastError return ?
-
yes. database is configured correctly. i have created tablemodel and do the insert, edit etc. but in other functions it shows database is not open. lastError returns "driver not loaded". I am using "QMYSQL" driver. i chekced the output of QSqlDatabase::drivers(). It returns "QSQLITE", "QMYSQL", "QMYSQL3", "QPSQL", "QPSQL7".
here is my two functions
// Record login and logouts
bool Database::AddLoginRecord(int usrID, int islogin) {
QSqlQuery * query = new QSqlQuery(dbCon);
// msg->Error(query->lastError());
qDebug() << QSqlDatabase::drivers();
query->prepare("INSERT INTOlogin_session
VaLUES (NULL,?,?)");
query->addBindValue(usrID);
query->addBindValue(islogin);return query->exec();
}
// Create table model for given table
QSqlTableModel * Database::CreateModel(QString table) {
QSqlTableModel * model = new QSqlTableModel(0,dbCon);
model->setTable('' + table + '
');
return model;
}create model function works fine and i can accesse database. but when addLoginRecord called i got the error "database not open"
-
Driver not loaded ? Do you have MySQL installed properly ?
-
Thanks for sharing your solution !
Since you have it working now, please update the thread title prepending [solved] so other forum users may know a solution has been found :)