CREATE, INSERT, SELECT, UPDATE Perl script and PostgreSQL database examples

Simple perl to postgresql examples:

#!/usr/bin/perl

#load perl postgresql module
use DBI;

$postgresql_database=yourpostgresdatabase;
$postgresql_user=yourpostgresuser;
$postgresql_password=yourpostgrespass;
$postgresql_host=localhost;

# connect to perl to postgresql database
my $dbh = DBI->connect(”DBI:Pg:dbname=$postgresql_database;
host=$postgresql_host”, “$postgresql_user”, “$postgresql_password”);

# EXAMPLE PERL and POSTGRESQL
# CREATE TABLE EXAMPLE
$sth = $dbh->prepare(”CREATE TABLE perl_postgres
( language varchar(40), version integer);”);
$sth->execute();

# EXAMPLE PERL and POSTGRESQL
# INSERT DATA INTO TABLE
$dbh->do(”INSERT INTO perl_postgres(language, version)
VALUES (?, ?)”, undef, (’perl’, 5)) or die (”Cannot INSERT”);

# EXAMPLE PERL and POSTGRESQL
# SELECT DATA FROM TABLE
$sth = $dbh->prepare(”SELECT language, version FROM perl_postgres”);
$sth->execute();
$row_reference = $sth->fetchrow_hashref();
print $row_reference->{’language’} . $row_reference->{’version’} . “\n”;

# EXAMPLE PERL and POSTGRESQL
# UPDATE DATA WITHIN TABLE
$sth = $dbh->prepare(”UPDATE perl_postgres SET version=’6′ where language=’perl’”);
$sth->execute();

Leave a Reply

You must be logged in to post a comment.