Re: binding values to sql statement in DBI perl

From: Deepblues <deepblues(at)gmail(dot)com>
To: Michael Fuhr <mike(at)fuhr(dot)org>
Cc: pgsql-novice(at)postgresql(dot)org
Subject: Re: binding values to sql statement in DBI perl
Date: 2005-04-07 16:26:59
Message-ID: bd6cfb9e050407092672558175@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Here is the code I'm trying to execute:
I'm accepting user input for the semester and gathering the data for that
particular semester.

print " Enter the semester:\n";
$f_semester = <STDIN>;
# collecting rows of section,section_loc table to insert into interface
table

my $sth = $dbh->prepare('SELECT course_id,
course_name,section.section_id,day,time,enroll_capacity,room_capacity,bldg,instructor_id,instructor_name,semester,notes
FROM section,section_location where section.section_id =
section_location.section_id AND section.semester=?'
)

or die "couldn;t prepare statement:".$dbh->errstr;

$sth->bind_param(1, $f_semester);
my @data;
$sth->execute($f_semester);

while(@data = $sth->fetchrow_array())
{

my $insert_csv = "INSERT INTO
interface_write(course_id,course_name,credit_hours,section_id,days,time,cap_enroll,room_cap,bldg,instructor_id,instructor_name,semester,notes)values(?,?,?,?,?,?,?,?,?,?,?,?,?)";
$dbh->do($insert_csv,undef,$data[0],$data[1],$data[2],$data[3],$data[4],$data[5],$data[6],$data[7],$data[8],$data[9],$data[10],$data[1
1],$data[12]);
}

$sth->finish;
}

I first tried it without passing the variable to $sth->execute;
It doesn;t display any error but it seems like it is not entering the while
loop.
I am new to postgres and dbi perl , and i'm tryinng to figure this one out.
Is there any module that has to be included to use the bind_param . I
already included
use DBI qw(:sql_types);
I also need to include the semester in single quotes to search in the table,
will bind_param enclose the text in quotes or how does it work.
_
Any help on this would be greatly appreciated.

Thanks and regards
Deepblues

On Apr 6, 2005 7:28 PM, Michael Fuhr <mike(at)fuhr(dot)org> wrote:
> On Wed, Apr 06, 2005 at 11:42:55AM -0500, Deepblues wrote:
> >
> > I am having trouble with binding values to SQL statements using dbi
perl.
> >
> > The scenerio is as follows :
> >
> > I have a scheduling database that holds the listings of classes and
> > there schedules for a university. I am trying to query the database to
> > display the listings of classes , sections for a particular semester
> > that the user enters.
> > I am able to get the results with the actual value but then when I use
> > the variable which holds the values of the semester the user entered
> > and try querying , it displays an error message saying
> >
> > "unbound place holder "
>
> Could you post a small but complete program that exhibits the
> problem? Without seeing your code we can only guess what might be
> wrong. Also, please post the complete, exact text of the error
> message (copied and pasted, not typed manually).
>
> I suspect the real error is something like the following:
>
> DBD::Pg::st execute failed: execute called with an unbound placeholder at
./foo line 14.
>
> If that's the case, then you might have called $sth->execute() with
> no arguments when you should have called it with arguments, e.g.,
> $sth->execute($variable). But that's just a guess; without seeing
> your code we can't be sure.
>
> --
> Michael Fuhr
> http://www.fuhr.org/~mfuhr/
>

--
Deepti Mandava
Graduate Teaching Assistant,
The University of Kansas
Lawrence, KS 66045-7546

In response to

Responses

Browse pgsql-novice by date

  From Date Subject
Next Message Keith Worthington 2005-04-07 16:30:27 Re: JOIN on a lookup table
Previous Message DavidF 2005-04-07 16:23:21 pg_restore returns error schema objects already exist