Microsoft Word - Sam's Teach Yourself MySQL in 21 Days - SAMS.doc

(singke) #1

  1. MySQL and mSQL (either of mSQL 1 or mSQL 2)

  2. mSQL 1 and mSQL 2

  3. MySQL, mSQL 1 and mSQL 2


The default choice is 3. This is probably the easiest choice to make. mSQL is an earlier
predecessor to MySQL. You will not harm anything by adding this driver to your system.
After you make your selection, the installation program prompts you with some more
questions. The first of these questions ask if you want to keep any previous modules. If
you have installed other modules on your system and are still using them, select Yes; if
not, select No. You will then be prompted for the directory where you installed MySQL. If
you installed MySQL into the default directory (/usr/local/mysql), you can just press
Enter. The next few questions pertain to a database that this driver can test itself against.
If you still have the test database in your MySQL installation, you can press Enter for
these questions, taking the default. If not, you must enter a database for the driver to test
against. You will also have to give a username and password to use, as well as a host
name.

After you have completed this step, and no errors are generated, then you have installed
the driver successfully and are ready to begin writing Perl scripts to access your
database.
You have now installed the tools you need to take advantage of this powerful technology. A word of
caution, a lot of books do not mention how to install the various drivers and so on onto a machine
because they fear that by doing so they are locked into that version. This is partly true. Most installation
techniques do not change often. If they do, it is generally to help the ease the process. The best advice
is to view the README documents before you install anything to make sure that you are using the latest
guidelines.
You can also use the PPM to install the driver. To do this, start the PPM by double-clicking it. When the
window is open, type the following at the command line:
PPM> install DBD-Mysql

If the driver cannot be found under this name, you can do a search for it. To do this, type the following
from the command line:
PPM> search mysql
This will return the most current driver name. From there, you can just type install drivername
and let the PPM do the rest for you.

Creating Your First Perl DBI/DBD Script


This chapter assumes that you have a basic knowledge of Perl. If you do not, you may want to read some
books that provide you with more detail about the language. Even if you don't know Perl, you can still read
on to see how useful and powerful this language is.


As discussed in previous chapters, there is a common thread with all interfaces. The Perl DBI is no
different. A connection has to be made, queries have to be passed to the database, results are
returned, and then the connection is closed—these are the general steps that all database interfaces
share.

The Perl DBI Object


The Perl DBI is made up of one object—the DBI object. From this object, you can find out what drivers are
available with the data_sources() method or you can create a database handle with the connect()
method The connect() method returns a database handle through which you can enter statements. A
database handle can be thought of as a type of connection. For example, when you make a phone call, you
dial the numbers on the phone to make a call. Someone on the other end answers and you can begin a
conversation. The connection you have with the other person is similar to a handle, and the telephone
company is like the DBI. You make a connection through the phone company much like you make a
connection through the DBI. If all your data is correct, you have a good connection. You can have a
conversation and then hang up. These same things occur with the DBI. The DBI can generate many handles
to different databases, much like the telephone company can connect you to a lot of people with your phone.


The DBI is a very powerful interface because it can generate as many handles as needed, and they all
don't have to be from the same database. For example, if you need to swap data between databases,
Free download pdf