List:General Discussion« Previous MessageNext Message »
From:Paul DuBois Date:February 19 2003 5:45pm
Subject:Re: C API Question
View as plain text  
At 11:13 -0600 2/19/03, Paul DuBois wrote:
>At 10:57 -0600 2/19/03, William R. Mattil wrote:
>>Hello,
>>
>>I am having some cockpit trouble with the following
>>
>>MYSQL_RES *mysql_list_fields(MYSQL *mysql, const char *table, const char
>>*wild);
>>
>>and it is like lack of understanding on my part. Syntax is:
>>
>>if (mysql_list_fields( &mysql, "some table name", "some field name"))
>>
>>mysql_store_result and mysql_fetch_row follow but nothing is ever
>>returned. If I replace the mysql_list_fields with:
>>
>>if (mysql_query(&mysql,"Describe some_table_name"))
>>
>>everything works. Where am I missing the boat here ?
>
>Nothing.  It's just that the documentation for this function is unclear/
>incorrect.  I just came to realize this myself a few weeks ago. :-(
>
>The information that mysql_list_fields() returns about the columns is
>returned in the result set *metadata*.  So what you should do is call
>mysql_fetch_field() to retrieve the metadata for each column of the result
>set.
>
>Note that the max_length value will always be zero.
>
>>
>>Thanks
>>
>>Bill
>>--
>>
>>William R. Mattil       | Statisticians define a lottery as a tax  
>>Sr. System Aministrator | on not understanding mathematics
>>(972) 399-4106          |

To follow up on my own posting:

Here's an example.  It shows how to retrieve various bits of metadata,
including the column metadata. It assumes tbl_name is a string
containing the table name.

MYSQL_ROW       row;
MYSQL_FIELD     *field;
unsigned long   *length;
unsigned int    i;

MYSQL_RES   *res_set = mysql_list_fields (conn, tbl_name, NULL);

if (res_set == NULL)
     fprintf (stderr, "list_fields failed\n");
else
{
     printf ("Number of columns: %d\n", mysql_num_fields (res_set));
     printf ("Number of rows: %d\n", mysql_num_rows (res_set));
     printf ("   %-12s %-12s", "name", "table");
     printf (" %-12s %3s %3s %4s %4s %s\n",
                 "default", "len", "max", "type", "dec", "not null");
     for (i = 0; i < mysql_num_fields (res_set); i++)
     {
         field = mysql_fetch_field (res_set);
         printf ("%2u %-12s %-12s",
             i,
             field->name,
             field->table ? field->table : "NULL");
         printf (" %-12s %3u %3u %3u %3u %0x %3d\n",
             field->def ? field->def : "NULL",
             field->length,
             field->max_length,
             field->type,
             field->decimals,
             field->flags,
             IS_NOT_NULL(field->flags)
         );
     }
}

mysql_free_result (res_set);
Thread
C API QuestionWilliam R. Mattil19 Feb
  • Re: C API QuestionPaul DuBois19 Feb
    • Re: C API QuestionPaul DuBois19 Feb