PDA

View Full Version : Querying within Threads



bera82
29th March 2006, 13:15
Hi,
I am using Qt3.3 and i am geting segmentation fault when i try to query within my thread!! I am trying to implement threads as i want the GUI to be responsive when querying is being done. The function which does the query is defined within a class which has a macro Q_OBJECT cause other functions in that class need a timer for database insertion. I have created a separate thread class by sub classing QThread. I have then over ridden the run() function to call this query method.

Please help!!
Thanking you,
Regards,
Bera

vratojr
29th March 2006, 13:52
Please,post the code.

jacek
29th March 2006, 14:09
http://doc.trolltech.com/3.3/threads.html

bera82
29th March 2006, 14:11
Sorry dude,
I can't post the code!! :( :(
Its illegal as i am working for a firm..
It would be very helpful if u could provide me with some suggestions..
Thanks.

wysota
29th March 2006, 14:16
Sorry dude,
I can't post the code!! :( :(
Its illegal as i am working for a firm..
It would be very helpful if u could provide me with some suggestions..
Thanks.

I'm sure posting a few lines of code won't hurt. If you don't want to provide a sample of your code, provide a minimal compilable example which reproduces the problem.

bera82
29th March 2006, 14:59
This is the function that gets called within the run() function of the thread class.
The moment it reaches "resA.exec()" it gives segmentation fault.

resA/resB/resC.... is of type QSqlQuery.
resPrevQueryA/.... is also of type QSqlQuery

This same function works beautifully fine without threads but hangs the GUI!!

Thanks,
bera


void CHistData::queryDBase()
{

QSqlDatabase *dbase = QSqlDatabase::addDatabase("QMYSQL3" );

dbase->setDatabaseName( "HMIDATABASE" );
dbase->setUserName( "" );
dbase->setPassword( "");
dbase->setHostName( "");
if( !dbase->open() )
{
dbase->lastError().showMessage( "An error occured. Please read the README file in the sqltable"
"dir for more information.\n\n" );
printf("Unable to open DB");
}
else
printf(" opened DB");


QString strFromTime=qplot->fromTimeStr;
QString strToTime=qplot->toTimeStr;

int plotScreenXPixelCounter = 0;

QString strFromDateTime=qplot->fromDateStr;
QString strToDateTime=qplot->toDateStr;


strFromDateTime.append(" ");
strToDateTime.append(" ");

// Store from & to - datetime specified by user in QString
strFromDateTime.append(strFromTime);
strToDateTime.append(strToTime);

// Store from & to datetime specified by user in string datatype of sql
QString fromDateTime = strFromDateTime;
QString toDateTime = strToDateTime;

// //cout<<"FROM DateTime before query: "<<fromDateTime<<endl;
// //cout<<"TO DateTime before query: "<<toDateTime<<endl;
//qplot->databaseEnabled = FALSE;

// create QString object to store FGU-FSM ID selected by user
QString fguFsmID;
//fguFsmID ="";
fguFsmID.append(qplot->options.FGUSel);
fguFsmID.append(qplot->options.FSMSel);

// //cout<<" FGUId check"<<fguFsmID<<endl;
QString cName;
// cName.append(' ');
cName.append(fguFsmID);


// Query to retrieve records for given From and To date
cName.append("A");
QString retrieveA = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime ";
//////////////////////////////////////////////
// qApp->processEvents();

printf("before resA.exec()\n");

resA=retrieveA;
resA.exec();

printf("after resA.exec()\n");

// //cout<<"querySize: checking resA"<<resA.size()<<endl;
////////////////////////////
// Query to retrieve records when given From Date does not have a record+
QString prevA = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime desc limit 1";

//qApp->processEvents();

resPrevQueryA=prevA;
resPrevQueryA.exec();
// //cout<<"querySize: checking resPrevQueryA"<<resPrevQueryA.size()<<endl;

cName.truncate(5);
cName.append("B");
// //cout<<"----------------------------"<<cName<<endl;

// Query to retrieve records for given From and To date
QString retrieveB = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime";

//qApp->processEvents();

resB=retrieveB;
resB.exec();
// //cout<<"querySize: checking resB"<<resB.size()<<endl;

// Query to retrieve records when given From Date does not have a record+
QString prevB = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime desc limit 1";

//qApp->processEvents();

resPrevQueryB=prevB;
resPrevQueryB.exec();

//cout<<"querySize: checking resPrevQueryB"<<resPrevQueryB.size()<<endl;

cName.truncate(5);
cName.append("C");

// Query to retrieve records for given From and To date
QString retrieveC = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime";

//qApp->processEvents();

resC=retrieveC;
resC.exec();

//cout<<"querySize: checking resC"<<resC.size()<<endl;

// Query to retrieve records when given From Date does not have a record+
QString prevC = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime desc limit 1";

//qApp->processEvents();

resPrevQueryC=prevC;
resPrevQueryC.exec();

//cout<<"querySize: checking resPrevQueryC"<<resPrevQueryC.size()<<endl;

cName.truncate(5);
cName.append("D");

// Query to retrieve records for given From and To date
QString retrieveD = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime ";

//qApp->processEvents();

resD=retrieveD;
resD.exec();

//cout<<"querySize: checking resD"<<resD.size()<<endl;

// Query to retrieve records when given From Date does not have a record+
QString prevD = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime desc limit 1";


//qApp->processEvents();

resPrevQueryD=prevD;
resPrevQueryD.exec();

//cout<<"querySize: checking resPrevQueryD"<<resPrevQueryD.size()<<endl;

cName.truncate(5);
cName.append("E");

// Query to retrieve records for given From and To date
QString retrieveE = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime ";

//qApp->processEvents();

resE=retrieveE;
resE.exec();

//cout<<"querySize: checking resE"<<resE.size()<<endl;

// Query to retrieve records when given From Date does not have a record+
QString prevE = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime desc limit 1";
//cout<<fguFsmID<<endl;

//qApp->processEvents();

resPrevQueryE=prevE;
resPrevQueryE.exec();

//cout<<"querySize: checking resPrevQueryE"<<resPrevQueryE.size()<<endl;

cName.truncate(5);
cName.append("F");

// Query to retrieve records for given From and To date
QString retrieveF = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime ";

// qApp->processEvents();

resF=retrieveF;
resF.exec();

//cout<<"querySize : checking resF"<<resF.size()<<endl;

// Query to retrieve records when given From Date does not have a record+
QString prevF = "select * from FreqTab where ChName = '"+cName+"' and ChDateTime between '"+fromDateTime+"' and '"+toDateTime+"' order by ChDateTime desc limit 1";

// qApp->processEvents();

resPrevQueryF=prevF;
resPrevQueryF.exec();

//cout<<"querySize: checking "<<resPrevQueryF.size()<<endl;
dbase->close();
}

bera82
29th March 2006, 15:01
Hey hangs the GUI as in, GUI freezes untill query is over.. then everything is fine..
Help me out pals..
Thanks

jacek
29th March 2006, 15:03
In what thread does the qplot object live?

PS. Please, at least remove the unnecessary comments.

jacek
29th March 2006, 15:04
Hey hangs the GUI as in, GUI freezes untill query is over.. then everything is fine..
How do you start that thread? Do you invoke start()?

bera82
29th March 2006, 15:10
Hey sorry for not removing the comments!!

qplot is an object of the ui file which is a plotting application.
In the above code there are lineEdits for from and to dateTime which are entered by the user and accessed here.

The thread is invoked by the start() method from the UI file itself when the user enters the From & To datetime and presses submit. In the submit function the thread is started and the above posted code runs.

Thank u for ur valuable time
Bera.

jacek
29th March 2006, 15:27
qplot is an object of the ui file which is a plotting application.
In the above code there are lineEdits for from and to dateTime which are entered by the user and accessed here.
Then read this carefully: http://doc.trolltech.com/3.3/threads.html
Especially the part about classes that use implicit sharing (like QString) and about classes derived from QObject.

pdoria
12th March 2008, 02:08
Hey bera82

Remember that each connection to the DB must be uniquely named ...

I use the following when working with dbconns inside threads:


// create a unique database connection to serve this thread
// two calls to rand() are a bit expensive but *effective*...
// note: this is for a PostgreSQL connection ..
sprintf(dbUnique, "myDB%x%x", rand(), rand() );
myDB = QSqlDatabase::addDatabase ( "QPSQL", dbUnique );
// set needed conn parameters here...

// whatever code...
QSqlQuery query(myDB); // this is the important part: access it by its unique name.

// remember to close after you're done... (or upon the thread's destruction)
myDB.close();


HTH,
Pedro Doria Meunier