The general process of establishing a MYSQL client program [group chart] MySQL provides a client library written in C programming language, which can be used to compile a client program for accessing MySQL. This library defines the application programming interface, including the following utility:
■ Establish and terminate connection management routines with server sessions.
■ Construct a query routine, send the routine to the server, and process the result.
■ When other c api calls fail, determine the status of the error accurate cause and the error reporting function.
This chapter describes how to use the client library to write your own programs. The main points we should remember are the consistency between our programs and the existing client programs in the MySQL distribution package, and the code's adequacy and reusability. This chapter assumes that you have some knowledge about C programming, but not necessarily an expert. This chapter develops a series of client programs from simple to complex. This process is the first part of the development of a program framework, which can not do anything except connect to and disconnect from the server. The reason for doing so is that, even though the MySQL client program is written for different purposes, they all have one thing in common: creating a connection to the server.
We will use the following steps to establish this program framework:
1. write some brief connection and disconnection code (client program 1 ).
2. add some error checks (client program 2 ).
3. modularized and reusable connection code (client program 3 ).
4. added the ability to obtain runtime connection parameters (host, user, password) (client program 4 ).
This framework is generally reasonable and can be used as the basis for writing any number of client programs. After developing it, we will not consider how to deal with various problems. First, we will discuss how to deal with specific hard-coded SQL statements, and then develop code for processing arbitrary statements. After that, add the query processing code to the client program framework and develop another program (Client Program 5), which is similar to the mysql client program. We will also consider (and solve) some common problems, such as "how to obtain the structure information about the table ?" And "how to insert images into the database ?" This chapter only discusses the functions and data types of the client library when necessary. For a list of all functions and types, see Appendix F "c api reference ". This appendix can be used as a reference for further background information using any part of the client library.
The sample program can be downloaded online and used directly without having to type them. For instructions, see Appendix A "obtain and install software ".
Where to find an example
A common problem with the MySQL mail list is "where can I find some client samples written in C ?". Of course, the answer is "in this book !". However, many people seem to have not considered that the MySQL distribution package contains several client programs (such as mysql, mysqladmin, and mysqldump), which are mostly written in C. Because this distribution package can be easily used as a source program, MySQL provides very few sample client code. Therefore, if you have not done so, find a time to find the source program distribution package and view these programs in the client directory. The MySQL client program is a shared software, from which code can be freely borrowed from its own program.
You can find code similar to what you want to do when writing your own program between the sample provided in this chapter and the client program included in the MySQL distribution package. In this case, you can copy and modify existing programs to reuse the code. Read this chapter to learn how the client library works. However, remember that you do not always need to write every trivial thing on your own (you will note that the reusability of the code is one of the purposes in the discussion of programming in this chapter ). By using programs compiled by others, you can avoid a lot of work, which is the best.
6.1 General process of creating a client program
This section describes how to use the MySQL client library to compile and connect programs. Different systems have different commands for creating client programs. you may need to modify the commands described here. However, the description here is generic and should be used in almost any client program you write.
6.1.1 basic system requirements
When you use C to compile a MySQL client program, a C compilation program is obviously required. The example described here uses gcc. In addition to source files, the following programs are required:
■ MySQL header file.
■ MySQL client library.
The MySQL header file and the client library form the support program for client programming. They may have been installed on your system. If not, obtain them. If MySQL is installed from the source program distribution package or binary distribution package, the client programmable support program should have been installed as part of the processing. If MySQL is installed from the RPM file, it is not supported unless the development program r p m is installed. To install the MySQL header file and library, see Appendix.