Database Reference
In-Depth Information
Despite these factors that compromise portability to some extent, the general portability
characteristics of the two-level architecture provide significant benefits for MySQL de‐
velopers.
Another characteristic common to the APIs used in this topic is that they are object
oriented. Whether you write in Perl, Ruby, PHP, Python, or Java, the operation that
connects to the MySQL server returns an object that enables you to process statements
in an object-oriented manner. For example, when you connect to the database server,
you get a database connection object with which to further interact with the server. The
interfaces also provide objects for statements, result sets, metadata, and so forth.
Now let's see how to use these programming interfaces to perform the most fundamental
MySQL operations: connecting to and disconnecting from the server.
2.1. Connecting, Selecting a Database, and Disconnecting
Problem
You need to establish a connection to the database server and shut down the connection
when you're done.
Solution
Each API provides routines for connecting and disconnecting. The connection routines
require that you provide parameters specifying the host on which the MySQL server is
running and the MySQL account to use. You can also select a default database.
Discussion
This section shows how to perform some fundamental operations common to most
MySQL programs:
Establishing a connection to the MySQL server
Every program that uses MySQL does this, no matter which API you use. The details
on specifying connection parameters vary between APIs, and some APIs provide
more flexibility than others. However, there are many common parameters, such
as the host on which the server is running, and the username and password of the
MySQL account to use for accessing the server.
Selecting a database
Most MySQL programs select a default database.
Disconnecting from the server
Each API provides a way to close an open connection. It's best to do so as soon as
you're done using the server. If your program holds the connection open longer
than necessary, the server cannot free up resources allocated to servicing the con‐
Search WWH ::




Custom Search