Chapter 17. 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 data types for the C API.
For the examples in this chapter, I have used a database for a fictitious
computer support business. 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 use the C API.
Connecting to MySQL
When writing a C program to interact with MySQL, you first need to prepare variables that will store data necessary for a MySQL connection and query results, and then you need to establish a connection to MySQL. To do this easily, you need to include a couple of C header files (as shown in the code example): 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 stdio.h is surrounded by
< and > symbols, C is instructed to look for it in the default location ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access