List:General Discussion« Previous MessageNext Message »
From:Thimble Smith Date:September 15 1999 7:35pm
Subject:Re: content of a column
View as plain text  
At 13:36, 19990915, Juan Manuel Doren wrote:
>how can I obtain the content of an especific column (field) using gnu C?
>
>I´m trying
>
> sprintf(lpzSelect,"SELECT * FROM NavAreas WHERE Padre = %i", padre);
> mysql_query(&mysql,lpzSelect);
> result = mysql_use_result(&mysql);
>
> while ( (row=mysql_fetch_row(result))  )
> {
>---------> HERE is where i need some help to print the content of the
>column "nombre"
>
> }

You have to know the index into the row of that column.  You can hard
code it (i.e., you know that "nombre" is always the third column in
the result set, so you use row[2] to access its value), or you can use
something like:

        MYSQL_FIELD *field;
        int nombre_field = 0;
        while((field = mysql_fetch_field(result)))
        {
            if (strcmp(field->name, "nombre") == 0)
                break;

            ++nombre_field;
        }

        if (nombre_field == mysql_field_count(result))
            return ERR_NO_NOMBRE_FIELD;

        while (row=mysql_fetch_row(result))
        {
            printf("Nombre: %s\n", row[nombre_field]); /* nombre isn't BLOB */
        }

If you were using C++, you could put the field->name => index into a
map<string, unsigned int> and then lookup indexes with that.  But if
you were using C++, you'd be using mysql++ and it'd be done already for
you.

I'd recommend that you not use SELECT * in your code (what happens if you
add/delete a column in the table?), and that you just hard-code the offset
(i.e., use #define NOMBRE_FIELD 2; row[NOMBRE_FIELD];).

Tim
Thread
content of a columnJuan Manuel Doren15 Sep
  • Re: content of a columnThimble Smith15 Sep