Database Reference
In-Depth Information
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:
Search WWH ::




Custom Search