Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I get started?

Perl Database Resources

How do I get started?

by  MikeLacey  Posted    (Edited  )
Please browse through faq219-2884 and faq219-2889 first. Comments on this FAQ and the General FAQ's are very welcome.

Contributions to this FAQ welcome, esp for different platforms and databases.

So - How Do I Get Started?

If you're running ActivePerl on Windows you're already more than halfway there. The DBI and several DBD's (DataBase Drivers) are included with the standard distribution.

If you're not running ActivePerl you will need to download the DBI and the DBD you need from www.cpan.org - you'll need an ANSI C compiler to build these modules, so the bundled compiler that comes with HPUX and Solaris won't do.

As an example, here are the steps needed to connect from a PC running Win95, Win98, WinME, NT4, Win2k, or WinXP to an Oracle database using the DBI.

1 Perl must be installed.
2 The DBI must be installed.
3 DBD::Oracle must be installed.
4 The Oracle client software must be installed.
5 SQL*Net must be configured. You must be able to connect to the database using tnsping or SQL*Plus.
6 Write a perl script that connects to the database

First three steps can be done in one go. Real easy.

1 Perl must be installed.
[tab]Install the lastest version of Perl from www.activestate.com

2 The DBI must be installed.
[tab]Install the lastest version of Perl from www.activestate.com

3 DBD::Oracle must be installed.
[tab]Install the lastest version of Perl from www.activestate.com

4 The Oracle client software must be installed.
[tab]Install the Oracle client software from www.oracle.com (free download, but license may be required depending on your platform)

5 SQL*Net must be configured. You must be able to connect to the database using tnsping or SQL*Plus.
[to be completed]

6 Write a perl script that connects to the database
[tt]
________________________________________________________________________________
This will read a table called emp with two columns, fname and lname.
[tab]use strict;
[tab]use DBI;

[tab]my ($dbh, $sth, @rows, $firstname, $lastname);

# connect to the database
[tab]$dbh = DBI->connect('dbi:Oracle:','scott', 'tiger')
[tab][tab]or die "Couldn't connect to database: " . DBI->errstr;

# prepare an SQL statement
[tab]$sth = $dbh->prepare('
[tab][tab]SELECT * FROM emp ORDER BY lname
[tab]') or die "Couldn't prepare statement: " . $dbh->errstr;

# execute the SQL statement
[tab]$sth->execute()
[tab][tab]or die "Couldn't execute statement: " . $dbh->errstr;
# Read the matching records and print them out
[tab]while (@data = $sth->fetchrow_array()) {
[tab][tab]$firstname = $data[1];
[tab][tab]$lastname = $data[2];
[tab][tab]print "\t$$firstname $lastname\n";
[tab]}

# disconnect from the database
[tab]$dbh->disconnect
[/tt]

Written in the hope that it may be helpful, contributions and comments welcome.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top