[solved] Application crash QSqlQuery(dbconnection) is called.
-
Hi. I have database connection named "sasm" defined in main.cpp. then in database.cpp i have assign connection to variable named dbCon. I successfuly created QSqlTabelModel using this variable. but when ever I used QSqlQuery(dbCon) application crashed. I couldn't find a solution. I am very grateful if someone can give solution soon. Thank you.
-
database.h
#ifndef DATABASE_H
#define DATABASE_H
#include <QtSql>
#include "message.h"
#include <QSqlQuery>class Database
{
public:
Database();~Database(); bool AddLoginRecord(int usrID, int islogin); QSqlTableModel * CreateModel (QString table);
private:
QSqlDatabase dbCon;Message msg;
};
#endif // DATABASE_H
database.cpp
#include "database.h"Database::Database(){
dbCon = QSqlDatabase::database("sasm");}
Database::~Database(){
dbCon.close();
}// Create table model for given table
QSqlTableModel * Database::CreateModel(QString table) {
QSqlTableModel * model = new QSqlTableModel(0,dbCon);
model->setTable('' + table + '
');
model->select();
return model;
}bool Database::AddLoginRecord(int usrID, int islogin) {
QSqlTableModel * model = new QSqlTableModel(0, dbCon);
return true;
}main.cpp // create database connection
bool CreateConnection() {
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL", "sasm");
db.setHostName("localhost");
db.setPassword("");
db.setUserName("");
db.setDatabaseName("sasm");
if (!db.open()) {
return false;
} else {
return true;
}
}when i Called createmodel function it works well. but any other function if i had call dbCon application will crash.
"Starting /media/sac/other/private/projects/SASM/build-SASM-Desktop_Qt_5_4_0_GCC_64bit-Debug/SASM...
The program has unexpectedly finished.
/media/sac/other/private/projects/SASM/build-SASM-Desktop_Qt_5_4_0_GCC_64bit-Debug/SASM crashed"
this is my error i got on qt creator -
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 :)