[GRASS-user] db_fetch questions

Hi Markus and Moritz,

Thanks for your suggestions; I tried another loop (the one from v.in.db) and gave the result that I needed. Below are my “experiments”:
The first db_fetch loop (exits after one interaction): I have to “cheat” to be able to get all the records from the database:

while(1){
if(db_fetch (&cursor, DB_NEXT, &more) != DB_OK)
return(-1);
nodeprop=db_get_value_as_double(dbvalue,ctype);
printf(“node prop= %.2f\n”,nodeprop);
count++;
if(count<12699) /* use max(cat) value for this one, just testing*/
more=0;
if ( more) break;
}

Now, using the loop as used in v.in.db I get what I need, without "cheating’:

while ( db_fetch (&cursor, DB_NEXT, &more ) == DB_OK && more ) {
nodeprop=db_get_value_as_double(dbvalue,ctype);
printf(“node prop= %.2f\n”,nodeprop);
}

There are two differences: on the first loop more==1 immediately, and thus exits the loop; while on the second one the loop is exptecting more==1. Another difference is the != or == DB_OK part.

What I can gather from this, is that the first loop is more appropriate to queries done on a category basis, but not for fetching an entire table.

Cheers,
Jaime

Moritz Lennert moritz.lennert@ulb.ac.be escribió:

On 20/03/08 00:19, Jaime Carrera wrote:

Hi list,

My apologies if this question fits better in the developers list.
I’m trying to understand how db_fetch works and why am getting a strange
error.
After a query is executed and db_open_select_cursor is used, db_fetch is
used to fetch the data of the resultant query. AFAIK, db_fetch(&cursor,
DB_NEXT, &more) fetches the data on a row by row basis; is this correct?

Yes.

My problem is that everything works fine on my program except the
db_fetch loop, as the loop breaks after the first iteration (although my
table has 3000 rows).
Can someone help me to figure out why the loop breaks?

Is your cursor correct ? Are you checking for DB_OK ? What is the status
of more ? You defininetely have to check for the former and possibly for
the latter (as in some of the examples Markus sent, i.e. v.in.db/main.c.)

In general, it would be helpful to send your code to understand what is
happening.

Moritz


¡Capacidad ilimitada de almacenamiento en tu correo!
No te preocupes más por el espacio de tu cuenta con Correo Yahoo!:
http://correo.espanol.yahoo.com/

On 20/03/08 14:31, Jaime Carrera wrote:

Hi Markus and Moritz,

Thanks for your suggestions; I tried another loop (the one from v.in.db) and gave the result that I needed. Below are my "experiments":
The first db_fetch loop (exits after one interaction): I have to "cheat" to be able to get all the records from the database:

  while(1){
    if(db_fetch (&cursor, DB_NEXT, &more) != DB_OK)
      return(-1);
    nodeprop=db_get_value_as_double(dbvalue,ctype);
    printf("node prop= %.2f\n",nodeprop);
    count++;
    if(count<12699) /* use max(cat) value for this one, just testing*/
       more=0;
    if ( more) break;
  }

Now, using the loop as used in v.in.db I get what I need, without "cheating':

   while ( db_fetch (&cursor, DB_NEXT, &more ) == DB_OK && more ) {
     nodeprop=db_get_value_as_double(dbvalue,ctype);
     printf("node prop= %.2f\n",nodeprop);
   }

There are two differences: on the first loop more==1 immediately, and thus exits the loop; while on the second one the loop is exptecting more==1. Another difference is the != or == DB_OK part.

What I can gather from this, is that the first loop is more appropriate to queries done on a category basis, but not for fetching an entire table.

I don't really understand the first one. Why do you break if more==1 ? Instead of

if(count<12699) /* use max(cat) value for this one, just testing*/
      more=0;
if ( more) break;

Shouldn't this just be

if(!more) break;

?

Moritz

Hi Moritz,

Yes… that one works too; I overlooked it. Now its more clear for me how it works.

Thanks,
Jaime

Moritz Lennert mlennert@club.worldonline.be escribió:

On 20/03/08 14:31, Jaime Carrera wrote:

Hi Markus and Moritz,

Thanks for your suggestions; I tried another loop (the one from v.in.db)
and gave the result that I needed. Below are my “experiments”:
The first db_fetch loop (exits after one interaction): I have to “cheat”
to be able to get all the records from the database:

while(1){
if(db_fetch (&cursor, DB_NEXT, &more) != DB_OK)
return(-1);
nodeprop=db_get_value_as_double(dbvalue,ctype);
printf(“node prop= %.2f\n”,nodeprop);
count++;
if(count<12699) /* use max(cat) value for this one, just testing*/
more=0;
if ( more) break;
}

Now, using the loop as used in v.in.db I get what I need, without
"cheating’:

while ( db_fetch (&cursor, DB_NEXT, &more ) == DB_OK && more ) {
nodeprop=db_get_value_as_double(dbvalue,ctype);
printf(“node prop= %.2f\n”,nodeprop);
}

There are two differences: on the first loop more==1 immediately, and
thus exits the loop; while on the second one the loop is exptecting
more==1. Another difference is the != or == DB_OK part.

What I can gather from this, is that the first loop is more appropriate
to queries done on a category basis, but not for fetching an entire table.

I don’t really understand the first one. Why do you break if more==1 ?
Instead of

if(count<12699) /* use max(cat) value for this one, just testing*/
more=0;
if ( more) break;

Shouldn’t this just be

if(!more) break;

?

Moritz


¡Capacidad ilimitada de almacenamiento en tu correo!
No te preocupes más por el espacio de tu cuenta con Correo Yahoo!:
http://correo.espanol.yahoo.com/