PDA

View Full Version : Sql Query Problem



MrShahi
30th May 2008, 08:23
Hello ,
I wana retreive value from database .so I wana execute this query.
SELECT * FROM Schedule WHERE StartDateTime <=now() and EndDateTime>=now();
for this I m using

QSqlQuery query;
query.prepare("SELECT *FROM Schedules WHERE StartDateTime<=:date ORDER BY ScheduleNo ASC");
query.bindValue(":date",currentSystemStartDate);
query.bindValue(":endTime",currentSystemEndTime);
query.exec();



if(query.isActive())

{

while(query.next())

{

startTime=query.value(2).toString();
}

}

But My problem is that it does not perform this query I mean to say it give all the value of table either it satisfy the condition or not ....................
So Please if any one can do something for me then I m very thankful for him.............

mazurekwrc
30th May 2008, 08:33
you want to have value that

StartDateTime <=now() and EndDateTime>=now()
but in query you check only first condition

query.prepare("SELECT *FROM Schedules WHERE StartDateTime<=:date ORDER BY ScheduleNo ASC");
you forgot add EndDateTime >= :endTime

MrShahi
30th May 2008, 08:53
thanx for reply .....
I m sorry I really forgot to write the actual code on that thread....
I m using this in my code but it is not working

QSqlQuery query;
query.prepare("SELECT *FROM Schedules WHERE StartDateTime<=:date EndDateTime>=:endTime ORDER BY ScheduleNo ASC");
query.bindValue(":date",currentSystemStartDate);
query.bindValue(":endTime",currentSystemEndTime);
query.exec();



if(query.isActive())

{

while(query.next())

{

startTime=query.value(2).toString();
}
}
Now can you tell me what mistake I m doing ?????????

mazurekwrc
30th May 2008, 09:02
there is still syntax mistake in your sql request
it should be


query.prepare("SELECT *FROM Schedules WHERE ( StartDateTime<=:date AND EndDateTime>=:endTime ) ORDER BY ScheduleNo ASC");

MrShahi
30th May 2008, 09:12
Again thanx Sir for your reply , but Sir it is still not working ................
It provides all the value of database ..........
I think may be the method to retrive the value is not right ......??????
Why it is not working ??????????????????

mazurekwrc
30th May 2008, 09:48
so use qDebug() and show what you get from

query->lastQuery();
currentSystemStartDate
currentSystemEndTime
and what you have in this fields: StartDateTime, EndDateTime

MrShahi
30th May 2008, 10:16
Thanx mazurekwrc ,
I solved my problem...................U r right I was doing mistake in Condition i.e currentSystemEndTime .............
Thanx once again...........................