List:General Discussion« Previous MessageNext Message »
From:Michael Widenius Date:January 29 2000 2:03pm
Subject:ANSWER: mysql_value(res,i,j) wanted :-)
View as plain text  
Hi!

>>>>> "Alexander" == Alexander Barkov <bar@stripped> writes:

Alexander> Hello!
Alexander> I need to access to mysql result in C program in this style.


Alexander> value=mysql_value(res,i,j),

Alexander> where "i" is row number and "j" is column number.
Alexander> It is required for unified code for many different
Alexander> databases.


Alexander> Surely, there is a chance to write something like this:

Alexander> mysql_data_seek(res,i);
Alexander> row=mysql_fetch_row(res);
Alexander> value=row[j];

Alexander> But mysql_data_seek seems to work very slow, especially
Alexander> with 10000, 20000 or above rows in result.

Alexander> Is there any better solution?

Alexander> Any help would be appreciated.

Alexander> Thanks.

The problem is that mysql_data_seek() searches sequentially after the
data.

What interface are you using to the other databases; I haven't heard
of anyone supporting a similar interface as the above.

On the other hand, you can quite easily create on for MySQL yourself
using mysql_row_seek(), which is very fast.

Something like:


MYSQL_ROW_OFFSET *values=mysql_init_values(mysql_result);

....

value=mysql_value(mysql_result,values,i,j);

.....
free((char*) values);


and the functions would look something like (note this isn't tested,
but as you are a C programmer, this shouldn't be a problem :)

MYSQL_ROW_OFFSET* mysql_init_values(MYSQL_RES *mysql_result)
{
  unsigned long long rows=mysql_num_rows(mysql_result),i;
  MYSQL_ROW_OFFSET *values=malloc(rows *sizeof(MYSQL_ROW_OFFSET));
  if (!values)
    return 0;     /* Error: Probably out of memory * /
  for (i=0 ; i < rows ; i++)
  {
    values[0]=mysql_row_tell(mysql_result);
    if (mysql_fetch_row(mysql_result))
    {		/* Should never happen, but ... */
       free((char*) values);
       return 0;
    }   
  }
  return values;
}
     
char *mysql_value(MYSQL_RES* mysql_result, MYSQL_ROW_OFFSET *values,
                  usigned int row, unsigned int column)
{
   /* No error checking ..;  We trust the caller. */
   mysql_row_seek(mysql_result,values[row]);
   return mysql_fetch_row(mysql_result)[column];
}


Regards,
Monty
Thread
mysql_value(res,i,j) wanted :-)(Alexander Barkov)28 Jan
  • Re: mysql_value(res,i,j) wanted :-)sinisa28 Jan
  • ANSWER: mysql_value(res,i,j) wanted :-)Michael Widenius29 Jan
    • Re: ANSWER: mysql_value(res,i,j) wanted :-)Benjamin Pflugmann30 Jan
      • Re: ANSWER: mysql_value(res,i,j) wanted :-)Michael Widenius30 Jan
        • Re: ANSWER: mysql_value(res,i,j) wanted :-)Benjamin Pflugmann30 Jan
          • Re: ANSWER: mysql_value(res,i,j) wanted :-)Michael Widenius5 Feb
  • dump errorkevin7 Feb
    • Re: dump errorsinisa7 Feb
  • DATE arithmaticKevinWaterson21 Feb
    • Re: DATE arithmaticsinisa21 Feb
    • Re: DATE arithmaticVivek Khera21 Feb
  • triggers ?KevinWaterson12 Mar
    • Re: triggers ?Jonathan Stimmel12 Mar