Developing MySQL Applications with Eclipse CDT
By: Mark Schoonover
In September, the Developer Zone featured Chapter 6 of Paul DuBois' MySQL, 3rd edition on Writing C Programs with the MySQL C API. This month, we continue in this series on developing MySQL applications in C with a tutorial from Mark Schoonover about using the Eclipse CDT (C Development Toolkit) as an IDE for developing C applications for MySQL.
Using Eclipse, we'll develop an example application that will document key structures, and demonstrate several API calls. This article will also cover configuring Ubuntu and Eclipse for MySQL client development. It's important that your development environment is tested and working properly before developing your MySQL client software.
You'll need to install the build-essential, Eclipse and the Eclipse CDT packages:
In order to develop an application with the API, you'll need libmysqlclient and all the header files installed.
For testing, you'll also need MySQL running with some test data. I've been experimenting and have been very happy with XAMPP. Outside of DBA work, I'm involved in web development and really enjoyed how quickly a development environment can be created under XAMPP. We'll use the sample world database for our program which can be downloaded from the document page of mysql.com
You'll need to create a project to store your configuration and source files. First, create a Managed Make C Project with File->New->Project->C->Managed Make C Project, and call it mysqlapidemo.
The Type of Project will be Executable (Gnu).
Click Finish. Eclipse will setup your workspace, and it should look like this:
Before you can compile our code, you need to include the path to the mysql header files, Project->Properties->C/C++ Build->Directories
and the mysqlclient, and Z libraries: Project->Properties->C/C++ Build->GCC C Linker->Libraries
Once that's done, we need to add a source file called - mysqlapidemo.c. Right click on the folder mysqlapidemo, then New->Source File
Make sure the file extension ends in c.
There are five data structures involved when writing your own clients using the API, but I'll discuss only three of them: MYSQL, MYSQL_ROW, and MYSQL_RES.
MYSQL: This data structure can be found in mysql.h, by searching for typdef struct st_mysql. It's a structure for storing all the details about MYSQL like communication parameters, server status, host, user, password, socket, server version, host info, etc. Understanding this structure will help greatly in developing your own clients. To browse this structure, open the /usr/include/mysql folder in the C/C++ Projects tab on the left of the
MYSQL_RES: Structure represents the rows returned from SQL statements that can return multiple rows of data. Search on typedef struct st_mysql_res in mysql.h for all the details.
MYSQL_ROW: A very small, but important structure that can handle a single binary row of data. Use the same technique as the previous structures to view the member variables. Search on typedef struct st_mysql_row for further information.
Other Related Articles
... to read more DBA articles, visit http://dba.fyicenter.com/article/