×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Perl Database Resources

How do I get started? by MikeLacey
Posted: 1 Apr 02 (Edited 17 Dec 02)

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.
    Install the lastest version of Perl from www.activestate.com

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

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

4  The Oracle client software must be installed.
    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

________________________________________________________________________________
This will read a table called emp with two columns, fname and lname.
    use strict;
    use DBI;

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

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

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

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

# disconnect from the database
    $dbh->disconnect

Written in the hope that it may be helpful, contributions and comments welcome.

Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close