May 28, 2018

Apples and Oranges, Part II: A Linux DBMS Comparison - page 7

The Database Design

  • November 18, 1999
  • By Matthias Warkus

MySQL's C API is pretty easy to use. The central element is a structure containing information about, and state of, the database connection, which is initialized by connecting to the MySQL server. A pointer to this structure must be passed to all MySQL client functions.

Queries are submitted as strings; this means that one must deal with C's string conversion facilities. Should data containing null bytes (\0) be used, the situation becomes more complicated, because a counted string is then passed instead of a C string.

To fetch a query's result, a pointer to a MYSQL_RES structure and a count variable are initialized with appropriate API functions, and then rows are fetched into a MYSQL_ROW variable, which is an array of strings. Directly putting results into integer variables, as PostgreSQL's implementation of ESQL could do, is not possible. The result buffer is then freed. As you can see, the semantics are almost the same as using a cursor in ESQL.

Here is list_books(), written using MySQL's C API:

  int count;
  MYSQL_RES *result;

  mysql_query(&bookstore, "SELECT ARTICLE_NO, AUTHOR_FIRST_NAMES,\
  result = mysql_store_result(&bookstore);

  for(count = mysql_num_rows(result); count > 0; count--)
	  MYSQL_ROW record;
	  record = mysql_fetch_row(result);

	  printf("\nArticle no. %s\n", record[0]);
	  printf("%s, %s:\n", record[2], record[1]);
	  printf("    %s (%s)\n", record[3], record[4]);
	  printf("Bought at %s; selling at %s; %s copies available\n\n",
			 record[5], record[6], record[7]);


The API functions are concisely, but sufficiently, documented in the Texinfo file that comes as MySQL's main source of documentation.

Most Popular LinuxPlanet Stories

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.