• Aucun résultat trouvé

Writing MySQL-Based Programs

Dans le document MySQL Cookbook (Page 53-56)

2.0. Introduction

This chapter discusses how to use MySQL from within the context of a general-purpose programming language. It covers basic application programming interface (API) op‐

erations that are fundamental to and form the basis for the programming recipes de‐

veloped in later chapters. These operations include connecting to the MySQL server, executing statements, and retrieving the results.

MySQL-based client programs can be written using many languages. This book covers the languages and interfaces shown in the following table (for information on obtaining the interface software, see the Preface):

Language Interface Perl Perl DBI

Ruby Ruby DBI

PHP PDO

Python DB API

Java JDBC

MySQL client APIs provide these capabilities, each covered in a section of this chapter:

Connecting to the MySQL server, selecting a database, and disconnecting from the server

Every program that uses MySQL must first establish a connection to the server.

Most programs also select a default database, and well-behaved MySQL programs close the connection to the server when they’re done with it.

25

Checking for errors

Any database operation can fail. If you should know how to find out when that occurs and why, you can take appropriate action such as terminating the program or informing the user of the problem.

Executing SQL statements and retrieving results

The point of connecting to a database server is to execute SQL statements. Each API provides at least one way to do this, as well as methods for processing statement results.

Handling special characters and NULL values in statements

Data values can be embedded directly in statement strings. However, some char‐

acters such as quotes and backslashes have special meaning, and their use requires certain precautions. The same is true for NULL values. If you handle these improp‐

erly, your programs will generate SQL statements that are erroneous or yield un‐

expected results. If you incorporate data from external sources into queries, your program might become subject to SQL injection attacks. Most APIs enable you to avoid these problems by using placeholders: refer to data values symbolically in a statement to be executed and supply those values separately. The API inserts them into the statement string after properly encoding any special characters or NULL values. Placeholders are also known as parameter markers.

Identifying NULL values in result sets

NULL values are special not only when you construct statements, but also in results returned from them. Each API provides a convention for recognizing and dealing with them.

No matter which programming language you use, it’s necessary to know how to perform each of the fundamental database API operations just described, so this chapter shows each operation in all five languages. Seeing how each API handles a given operation should help you see the correspondences between APIs more easily and better under‐

stand the recipes shown in the following chapters, even if they’re written in a language you don’t use much. (Later chapters usually implement recipes using only one or two languages.)

It may seem overwhelming to see each recipe in several languages if your interest is in only one particular API. If so, I advise you to read just the introductory recipe part that provides the general background, then go directly to the section for the language in which you’re interested. Skip the other languages; should you develop an interest in them later, come back and read about them then.

This chapter also discusses the following topics, which are not directly part of the MySQL APIs but help you use them more easily:

26 | Chapter 2: Writing MySQL-Based Programs

Writing library files

As you write program after program, you find that you carry out certain operations repeatedly. Library files enable encapsulating code for those operations so they can be performed easily from multiple scripts without repeating the code in each one.

This reduces code duplication and makes your programs more portable. This chapter shows how to write a library file for each API that includes a routine for connecting to the server—one operation that every program that uses MySQL must perform. Later chapters develop additional library routines for other operations.

Additional techniques for obtaining connection parameters

An early section on establishing connections to the MySQL server relies on con‐

nection parameters hardwired into the code. However, there are other (and better) ways to obtain parameters, ranging from storing them in a separate file to enabling the user to specify them at runtime.

To avoid manually typing in the example programs, get a copy of the recipes source distribution (see the Preface). Then, when an example says something like “create a file named xyz that contains the following information ...,” you can use the corresponding file from the recipes distribution. Most scripts for this chapter are located under the api directory; library files are located in the lib directory.

The primary table used for examples in this chapter is named profile. It first appears in Recipe 2.4, which you should know in case you skip around in the chapter and wonder where it came from. See also the section at the very end of the chapter about resetting the profile table to a known state for use in other chapters.

The programs discussed here can be run from the command line. For instructions on invoking programs for each language covered here, read “Executing Programs from the Command Line” on the com‐

panion website (see the Preface).

Assumptions

To use the material in this chapter most effectively, make sure to satisfy these require‐

ments:

• Install MySQL programming support for any languages that you plan to use (see the Preface).

• You should already have set up a MySQL user account for accessing the server and a database for executing SQL statements. As described in Recipe 1.1, the examples in this book use a MySQL account that has a username and password of cbuser and cbpass, and we’ll connect to a MySQL server running on the local host to access

2.0. Introduction | 27

a database named cookbook. To create the account or the database, see the instruc‐

tions in that recipe.

• The discussion here shows how to use each API language to perform database operations, but assumes a basic understanding of the language itself. If a recipe uses programming constructs with which you’re unfamiliar, consult a general reference for the language of interest.

• Proper execution of some of the programs might require that you set certain envi‐

ronment variables. General syntax for doing so is covered in “Executing Programs from the Command Line” on the companion website (see the Preface). For details about environment variables that apply specifically to library file locations, see Recipe 2.3.

Dans le document MySQL Cookbook (Page 53-56)