Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Setting up the dbi modules

by hda (Chaplain)
on May 30, 2009 at 08:05 UTC ( [id://767020]=note: print w/replies, xml ) Need Help??


in reply to Setting up the dbi modules

Hi,

I might not be the ideal person to answer this, as I am not an expert in the field of databases. I have however some daily experience accessing PostgreSQL databases from Perl programs, so I hope this will help you.
To begin with, we will only use DBI and assume you already have a user and a password, to access some table in some database. Then we can define:

use DBI; my $database = 'databasename'; my $dbuser = 'username'; my $usrpsswd = 'password'; my $dbtable = 'tablename';


Then you need to define a database connection, and a query (here defined for PostgreSQL):

my $dbconnection = DBI->connect("dbi:Pg:dbname=$database", $dbuser, $u +srpsswd);


Then you will need to prepare an SQL query for your connection:
my $query = $dbconnection->prepare(<<End_SQL); SELECT something1,something2 FROM $dbtable End_SQL

Then execute:

$query->execute();


Then you can make, for example, a while loop to catch all the things you asked for in your query:

while (($something1,$something2) = $query->fetchrow_array() ) { do your stuff here } # and then disconnect $dbconnection->disconnect();


These very simple things work for me. Be careful that I have not mentioned anything on secure connections and other advanced stuff. Other monks might be able to help on that. My lines only refer to very simple and basic database access.

Hope this helps!

Replies are listed 'Best First'.
Re^2: Setting up the dbi modules
by neutron (Sexton) on May 30, 2009 at 15:27 UTC
    hda, thanks for your reply. This looks pretty helpful. I'll give it a shot and see what happens!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://767020]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2024-04-24 09:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found