Question on stability of results returned by C api mysql_fetch_row()

Lew Pitcher <[email protected]> Thu, 11 Feb 2021 16:27:19 -0000 (UTC)
Newsgroups comp.databases.mysql
Organization The Pitcher Digital Freehold
Message-ID <[email protected]>
I have written a C program that summarizes the contents of a table. It 
only "SELECT"s from the table, and retrieves rows using mysql_fetch_row
(). As the program must interpret the row data, it may need to work with 
as many as five (5) rows as a time.

I'm chasing down a memory corruption problem with this program, that 
seems to overwrite the stored row data, and I want to eliminate my use 
(or possibly, misuse) of the C api as the source.

My question is: Once I've retrieved a row using mysql_fetch_row(), does 
that returned row (the MYSQL row, and the strings it's elements point to) 
remain stable and unaltered by subsequent mysql_fetch_row() calls? If so, 
then my problem lies outside of my use of the API, otherwise, I suspect 
that I've used the API incorrectly, and will have to refactor that 
portion of the code.

Here's a brief, naive example of the sort of processing I'm doing. This 
is NOT the code I'm debugging; this code doesn't seem to suffer the 
memory corruption my more complex program does. However, this code /does/ 
illustrate the mysql_fetch_row() assumption that I use in my bigger 
project.

  ##### Table definition #####
  Field	Type	Null	Key	Default	Extra
  t1ID	int(10) unsigned	NO	PRI	NULL	auto_increment
  t1Key	varchar(20)	NO		NULL	
  t1Valu	varchar(80)	YES		NULL	
  
  ##### Table contents #####
  t1ID	t1Key	t1Valu
  1	HOME	/home/lpitcher
  2	PWD	/home/lpitcher
  3	LOGNAME	lpitcher
  4	TERM	xterm
  
  ##### Program source #####
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  
  #include <mysql/my_global.h>
  #include <mysql/mysql.h>
  
  /*
  ** USER macro definition supplied by compile commandline
  ** PSWD macro definition supplied by compile commandline
  */
  
  int main(void)
  {
    MYSQL *dbm;
    MYSQL_RES *results;
    MYSQL_ROW row1,row2,row3;
  
    dbm = mysql_init(NULL);
    mysql_real_connect(dbm,"localhost",USER,PSWD,"lptest",0,NULL,0);
  
    mysql_query(dbm,"SELECT t1Key, t1Valu FROM lptest.t1;");
    results = mysql_store_result(dbm);
  
    row1 = mysql_fetch_row(results);
  
    /*
    ** Do either of these calls to mysql_fetch_row() somehow
    ** alter the results accessable from the row returned by
    ** the prior call(s) to mysql_fetch_row()?
    */
    row2 = mysql_fetch_row(results);
    row3 = mysql_fetch_row(results);
  
    /*
    ** At this point, can I be certain that row1, row2, and row3
    ** all access different table rows?
    */
  
    if (strcmp(row1[0],row2[0]) == 0)
      printf("First and second rows have the same key   [%s]\n",row1[0]);
    if (strcmp(row1[1],row2[1]) == 0)
      printf("First and second rows have the same value [%s]\n",row1[1]);
  
    if (strcmp(row1[0],row3[0]) == 0)
      printf("First and third rows have the same key    [%s]\n",row1[0]);
    if (strcmp(row1[1],row3[1]) == 0)
      printf("First and third rows have the same value  [%s]\n",row1[1]);
  
    if (strcmp(row2[0],row3[0]) == 0)
      printf("Second and third rows have the same key   [%s]\n",row2[0]);
    if (strcmp(row2[1],row3[1]) == 0)
      printf("Second and third rows have the same value [%s]\n",row2[1]);
  
    mysql_free_result(results);
    mysql_close(dbm);
  
    return 0;
  }
  
  ##### Program execution #####
  First and second rows have the same value [/home/lpitcher]


I appreciate any guidance or advice you can give me.
Thanks, 
-- 
Lew Pitcher
"In Skills, We Trust"