MySQL database technology (26) [figure] in this part of this book, we will discuss what we need to know to write our own programs to access the MySQL database. MySQL has a set of utilities. For example, mysqldump exports the context and structure definitions of a table. mysql import loads data files into the table, and mysqladmin manages w operations. mysql allows users to interact with the server to execute arbitrary queries. Every standard MySQL utility tends to be small, with a focus on the specific and limited functionality that a program can accomplish. Even in mysql, mysql is more flexible than other utilities, so it can be used to execute any number of queries, that is, it is designed to allow direct SQL queries to the server and view the query results.
The limited feature of the MySQL client is not a drawback, but is specially designed. Programs are utility programs with a general purpose; they do not try to anticipate all the possible needs you want to do. MySQL developers do not approve of writing large, bloated programs to try to do everything they may want (and the results will make the program contain a large number of things that you don't care about at all ). However, sometimes some applications do need to be unable to process the capabilities of conventional clients. To handle these situations, MySQL provides a client programming library. This allows you to write your own program to meet any specific requirements that your application may have. By allowing you to access the MySQL server, the openness of the client is limited only by your imagination.
How can I write my own program to obtain special capabilities? Let's compare the access to the mysql server by the MySQL client and its interface without additional code:
■ Custom input processing is supported. You can use mysql to enter the original SQL statement. With your own program, you can provide users with more intuitive and easy-to-use input methods. The user does not need to know s q l, or even the role of the database in the completed task.
The collection of input information may be a basic way to read prompts and values in the command line style, or it may be to use screen management packages (such as curses or S-L a n g), using the Tcl/Tk X Window or Web browser format based on the screen input as complex.
For most people, it is easier to specify the search parameters by entering a certain format than to publish a SELECT statement. For example, if a real estate agent is looking for a house with a certain price range, style, or location, you only need to input the search parameters into the table to get the matching content at the minimum price. This application is also considered when you enter a new record or update an existing record. The keyboard operator in the data input department should not need to know the SQL syntax such as INSERT, REPLACE or UPDATE.
Another reason for proposing the input collection layer between the end user and the MySQL server is that the input provided by the user can be verified. For example, you can check the data to confirm that they are in a MySQL-compliant format, or you can ask for a specific region.
■ You can customize the output. Mysql outputs are basically unformatted. you can select a tab as the separator or output in the form of a table. If you want to make the output result look better, you must format it yourself. These requirements may be as simple as printing "Missing" instead of "NULL", or more complex. Consider the following report: