MySQL C API - accessing rows by column name

Is there a way to access rows in a MySQL database using a column name?

For example, instead of using row[0] for the first column, you should use something like row['authors'] . I want to directly program using the C API without any wrappers, not C ++.

Thank you for your help!

+4
source share
1 answer

C does not actually support basic arrays of values, such as (I assume you think) PHP. The closest thing you could get is to get an array of column names, search for the one you need, and use this index. For instance:

 mysql_query(_MySQLConnection, query); MYSQL_RES *result = mysql_store_result(_MySQLConnection); unsigned int num_fields = mysql_num_fields(result); MYSQL_ROW row; MYSQL_FIELD *field; unsigned int name_field; char *field_name = "name"; char *headers[num_fields]; for(unsigned int i = 0; (field = mysql_fetch_field(result)); i++) { headers[i] = field->name; if (strcmp(field_name, headers[i]) == 0) { name_field = i; } } while ((row = mysql_fetch_row(result))) { //do something with row[name_field] printf("Name: %s\n", row[name_field]); } mysql_free_result(result); 
+11
source

Source: https://habr.com/ru/post/1338595/


All Articles