The general process of establishing a MySQL client program

Source: Internet
Author: User
Tags command line functions header include connect mysql mysql client query


MySQL provides a client library written in the C programming language that can be used to write client programs that access MySQL. This library defines the application programming interface, including the following utilities:
Establishes and terminates a connection management routine with a server session.
Constructs a routine for the query, sends the routines to the server, and processes the results.
When other C API calls fail, determine the status and error reporting function for the exact cause of the error.
This chapter describes how to write your own programs with the client library. Some of the key points to keep in mind are the consistency of your own programs with the client programs that are already in the MySQL distribution, the modularity and reusability of the code. This chapter assumes that you know some knowledge about programming with C, 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 do nothing but connect and disconnect from the server. The reason for this is that, although 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 build this program framework:
1. Write some brief code for the connection and disconnection (client program 1).
2. Add some error checking (client program 2).
3. Make the connection code modular and reusable (client program 3).
4. Increase the ability to obtain Run-time connection parameters (host, user, password) (client program 4).
This framework is generally reasonable and can be used as a basis for writing any number of client programs. After developing it, we will not consider how to deal with various problems. First, we'll discuss how to handle a particular hard-coded SQL statement, and then develop the code that handles any statement. After that, add the query processing code to the client program framework, and develop another program (client program 5) that resembles the MySQL client program. We will also consider (and solve) some common problems, such as "How do I get structured information about a table?" "and" How do I insert images in a database? "This chapter discusses the functions and data types of the client library only when it is needed." For a list of all functions and types, refer to Appendix F, "C API reference." You can use this appendix as a reference for further background information that uses any part of the client library.
The sample programs can be downloaded online and can be used directly, without having to type them again. For instructions, see Appendix A, "Getting and installing software."
Where to look for examples
A common problem with the MySQL mailing list is "Where can I find some client examples written in C?" ”。 Of course, the answer is "right here in this book!" ”。 However, many people do not seem to consider that the MySQL distribution includes a number of client programs (such as MySQL, Mysqladmin, and Mysqldump), most of which are written in C. Because this distribution can be easily used in the form of 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 and view the programs in the client directory. The MySQL client program is shareware, from where it can freely borrow code for its own program.
Between the examples provided in this chapter and the client programs included in the MySQL distribution, you can find code similar to what you want to do when you write your program. If so, you can reuse the code by copying and modifying existing programs. You should read this chapter to see how the client library works. Keep in mind, however, that you don't always have to write your own trivial things (you'll notice that the reusability of code is one of the purposes in this chapter's discussion of writing programs). You can avoid a lot of work by using other people's programmed programs, which is the best.
6.1 General procedures for establishing a client program
This section describes the steps that are included in compiling and connecting programs using the MySQL client library. The commands for setting up the client program for different systems are also different, and you may need to modify the commands described here slightly. However, the instructions here are generic and should be able to be used in almost any client program you write.
6.1.1 Basic System Requirements
When you write a MySQL client program in C, you will obviously need a C compiler. The sample illustrated here uses GCC. In addition to your own source files, you will need the following programs:
MySQL header file.
MySQL client library.
The MySQL header file and the client library compose the client programming support program. They may already be installed on your system. If not, you should get them. If MySQL is installed from a source program or binary distribution package, the client programmable support program should already be installed as part of the process. If MySQL is installed from the rpm file,
This support is not supported unless you have the development program R P M installed. If you need to install MySQL header files and libraries, see Appendix A.
6.1.2 Compiling and connecting client programs
To compile and connect the client program, you must specify the location of the MySQL header file and the client library, because they are not normally installed in the location of the compiler and connector default searches. For the following example, assume the location of the header file and client library is/usr/local/include/mysql and/usr/local/lib/mysql. To tell the compiler how to find the MySQL header file, send it a-i/usr/local/include/mysql parameter when compiling the source file as the destination file. For example, you can use a command such as:

To tell where the connector is, you can find the client library and its name, and when the connection target file produces an executable binary, transfer the-l/usr/local/lib/mysql and-lmysqlclient parameters as follows:

If the client program includes multiple files, the names of all destination files are listed on the connection command. If the connection step causes the error of the required floor () function to not be found, connect to the math library by adding-l m after the command line:

Additional libraries may also need to be added. For example,-LSOCKET-LNSL may be required on Solaris.
If you are not using make to establish a program, it is recommended that you understand how to do so that you do not have to manually type many of the commands that establish the program. Suppose there is a client program myclient, including two source files Main.c and aux.c, and a header file myclient.h. A simple makefile establishes the code for this program, as follows:

If it is a system that needs to be connected to the math library, change the value of the LIBS and add-lm to the last: LIBS =-l/usr/local/lib/mysql-lmysqlclient-lm
If you need additional libraries, such as-lsocket and-l n s L, you will also add these libraries to the Libs. Using M a k E F i l e, whenever you modify the He yuan file, simply type "M a K e" to re-establish the program code. That's easier than typing a long gcc command, and fewer errors occur.



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.