Re: Perl DBI question

From: greg(at)turnstep(dot)com
To: pgsql-admin(at)postgresql(dot)org
Cc: mkoval01(at)sprintspectrum(dot)com
Subject: Re: Perl DBI question
Date: 2003-06-18 17:47:59
Message-ID: c4f81faedbd5cdb090c86bacf5fce16b@biglumber.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-admin


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

You should not be using the "do" method, but the prepare/execute model
instead. Using placeholders can not only be more efficient but allows
quoting to happen properly. Here is a rewrite:

#--Establish the DB connection
#--Assign the DB name
my $dbName = 'checkbook';
my $dbuser = "joe";
my $dbpass = "sixpak";

my $dbh = DBI->connect("dbi:Pg:dbname=$dbName", $dbuser, $dbpass, {AutoCommit=>1, RaiseError=>1})
or die "Could not connect to the database: $DBI::errstr\n";

my $SQL = "
INSERT INTO transactions (date,description,amount,confirmation,nameid,typeid)
VALUES (? ,? ,? ,? ,? ,? )";

my $sth = $dbh->prepare($SQL);

my $count = $sth->execute($datePaid,$description,$amount, $confirmation,$nameid,$typeid);

print "Insert count: $count\n";

It is hard to tell why your inserts are going wrong without more information, but
the RaiseError should at least help catch some obvious errors.

- --
Greg Sabino Mullane greg(at)turnstep(dot)com
PGP Key: 0x14964AC8 200306181337

-----BEGIN PGP SIGNATURE-----
Comment: http://www.turnstep.com/pgp.html

iD8DBQE+8KUHvJuQZxSWSsgRAhWoAJ9/aw9AaCMa5vGvEpvujEEBm4iBXQCggSSw
jPaGcdMt9Qq9XeJqttvdX48=
=x1nu
-----END PGP SIGNATURE-----

In response to

Responses

Browse pgsql-admin by date

  From Date Subject
Next Message John McGloughlin 2003-06-18 17:53:23 Re: configure readline error
Previous Message Bruno Wolff III 2003-06-18 17:46:48 Re: Perl DBI question