The FreeBSD Diary |
![]() |
(TM) | Providing practical examples since 1998If you buy from Amazon USA, please support us by using this link. |
|
Accessing a mysql database from perl
9 April 2000
|
| In previous articles I wrote about how to install and configure mysql (a very popular database). In this article, I write about how to access a mysql database from within a perl program. This will not be a full tutorial on the perl/mysql interface. Rather, it just shows you what ports need to be installed. |
|
The install
|
| Remember, I have the entire ports tree
installed. So adding these ports were. See also, Installing a port without installing the ports. First, I just tried to connect to mysql, thinking that perhaps the required software was already installed. I mean, why bother installing without first checking. Nope. It was not installed. Here's the error message:
I didn't know what ports I needed in order to get at mysql from within perl. While I was at one of my resource sites, I found a reference to the DBI (which I think means DataBase-independent Interface; comments are welcome.) So I found and installed that port: cd /usr/ports/databases/p5-DBI make install Then I tried again. No luck:
So then I looked for a perl-mysql port. I found one and installed it: cd /usr/ports/databases/p5-Mysql make install And I tried again: $ perl connect.pl wocker march daniel dan dan1 Which means it worked. See the next section for the sample code: |
|
Sample code
|
For this test, I just wanted a very simple program which would connect to
the database, select some stuff from a table, and print it out. Here it is:
Where you must make the following substitutions:
You should also subsititue your own SQL. Perhaps you should also disconnect from the database. I haven't read the perl/mysql module yet. The above is based on the sample code I found on the web. |
|
Resources
|
|