[NTLUG:Discuss] Decipher C++ source

Preston Hagar prestonh at gmail.com
Mon Jun 8 16:36:03 CDT 2009


On Mon, Jun 8, 2009 at 2:49 PM, Michael Barnes<barnmichael at gmail.com> wrote:
> I have a MySQL database that logs telephone calls.  A module puts the
> CallerID number into a variable.  Then, this database query is
> supposed to return the last date and time that number called.  The
> database table has fields for NUMBER, NAME, CALL_DATE and CALL_TIME.
> (There are others, but I don't care about them for this purpose.)
>
> Anyhow, it appears that the query is giving me the first time the
> number called, not the latest time.
>
> I'm hoping some good programmer out there could take a peek at this
> and answer some questions.
> First I need to know why I get the first call instead of the last call.
The desc in the order by clause is only applying to CALL_TIME, not CALL_DATE

(here is a reference link:
http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html)

Try changing the order by line to the following:

order by CALL_DATE desc, CALL_TIME desc",


> Then some curiosity questions.
> What is the significance of the '!' after the NAME field?
The !  is for not.  What it is saying here is that it wants only call
records where the NAME column is not equal to "" or basically where
NAME is not empty.

> Is the value \"\"  for NAME some type of a variable or wildcard?
>

\"\"  is evaluated as "" when the query is run.  The \ are to escape
the " so that the C++ interpreter doesn't thing the sprintf string has
ended, but instead actually includes a "

> What line(s) actually select the proper data (last call) from the list
> returned by the query?
>
I added a few comments to the code below that might clarify things.

> Once it gets the right data, it puts it into an element in the
> BusDriver module.  It also increments the call counter, which is
> working correctly.
>
> Here's the code.
>
//  Construct SQL query
>  sql=QString().sprintf("select CALL_DATE,CALL_TIME from DETAILS_GAL \
>                         where (NUMBER=\"%s\")&&(NAME!=\"\") \
>                         order by CALL_DATE,CALL_TIME desc",
>                        (const char *)mon->showCode(),
>                        (const char *)pnum);

//  Query the database
>  q=new QSqlQuery(sql);
//  If the first record is valid (not null) then proceed
>  if(q->first()) {
>    mon->metaData(line)->
// Set the Bus Driver using the first column returned (value(0) which
is CALL_DATE) and the second
// value returned (value(1) which is CALL_TIME)
>      setElement(BusDriver::LastCallDatetimeElement,
>                 QString().sprintf("%d",QDateTime(q->value(0).toDate(),
>                                                  q->value(1).toTime()).
>                                   toTime_t()));
>  }
>  mon->metaData(line)->setElement(BusDriver::CounterElement,q->size());
>  delete q;
>
>

Hope this helps out.

Preston



More information about the Discuss mailing list