[solved] Application crash QSqlQuery(dbconnection) is called.
-
wrote on 21 Sept 2015, 15:01 last edited by sachi
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.
-
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.
wrote on 21 Sept 2015, 17:12 last edited by@sachi Can you show some code excerpts, please?
-
wrote on 21 Sept 2015, 17:33 last edited by
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 -
wrote on 21 Sept 2015, 17:58 last edited by
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 ? -
wrote on 22 Sept 2015, 15:14 last edited by
dbCon is initialized. if i use dbCon any function other than createmodel aplication crashed. I cant figure out whats wrong.
-
wrote on 22 Sept 2015, 18:56 last edited by
I found where i was wrong. I haven't initilized database object where this fuction was called. but right now dbCon is closed. i have open connection using dbCon.open(). but still dbCon.isOpen returns false.
-
Is dbCon correctly configured before calling open ? What does lastError return ?
-
wrote on 23 Sept 2015, 12:40 last edited by
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"
-
wrote on 23 Sept 2015, 19:27 last edited by
I just found problem is not with database class. it is with mainWindow class. if I create object using database class in mainWindow and called method from database.cpp. database connection show as invaild.
-
Driver not loaded ? Do you have MySQL installed properly ?
-
wrote on 25 Sept 2015, 13:49 last edited by
yeah i have solved problem. its was at the main function. i have created database connection after the MainWindow is created. so mainWindow cannot use the database connection. By the way thanks for your help guys.
-
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 :)
8/13