Chapter 14. C API

This chapter covers the C API provided by MySQL. The first part provides a basic tutorial on how to connect to MySQL and how to query MySQL with C and the C API. Following the tutorial is an alphabetical listing of MySQL functions in the C API with explanations and, in most cases, examples. At the end of this chapter is a listing of special datatypes for the C API. For the examples in this chapter, a database for a fictitious computer support business used. The database contains one table with client work requests (workreq) and another with client contact information (clients).

Using C with MySQL

This section presents the basic tasks you need to perform use the C API.

Connecting to MySQL

When writing a C program to interact with MySQL, first make a connection to MySQL. To do this easily, you need to include a couple of C header files: stdio.h for basic C functions and variables, and mysql.h for special MySQL functions and definitions. These two files come with C and MySQL, respectively; you shouldn't have to download them from the Web if both were installed properly.

   #include <stdio.h>
   #include "/usr/include/mysql/mysql.h"
   int main(int argc, char *argv[  ])
   {
           MYSQL *mysql;
           MYSQL_RES *result;
           MYSQL_ROW row;

Because of the < and > symbols surrounding stdio.h, C is instructed to look for it in the default location for C header files (e.g., /usr/include), or in the user's path. Because mysql.h may not be in the default locations, with the aide of double quotes, the absolute ...

Get MySQL in a Nutshell now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.