On Wed, Aug 18, 2010 at 19:55, Babale Fongo <bfo...@googlemail.com> wrote:
snip
> In the first example, DBI always passed the value for offset and ignored the
> second value after the comma.
>
> I'm now using the second code as it works without problem even if string
> contains space. I just need to understand why DBI behaves that way.
>
> I cannot hard code limit in the string because the value varies. This is
> used in a pager and the values of limit varies depending on which page is to
> be displayed.
snip

Placeholders represent single values.  The DBI will properly quote any
value it binds to the placeholder.  Therefore, you must use more than
one placeholder if you want to provide more than one value:

#!/usr/bin/perl

use strict;
use warnings;

use DBI;

unlink "foo.db" or die $! if -e "foo.db";
my $dbh = DBI->connect(
        "dbi:SQLite:dbname=foo.db",
        "",
        "",
        {
                ChopBlanks         => 1,
                PrintError         => 0,
                RaiseError         => 1,
                ShowErrorStatement => 1,
                FetchHashKeyName   => "NAME_lc",
        }
) or die DBI->errstr;

$dbh->do("CREATE TABLE foo (n INTEGER)");

my $insert = $dbh->prepare("INSERT INTO foo (n) VALUES (?)");

for my $n (1 .. 100) {
        $insert->execute($n);
}

my $select = $dbh->prepare("SELECT n FROM foo ORDER BY n LIMIT ?, ?");

my $count  = 5;
my $offset = 0;

do {
        $select->execute($offset, $count);
        my @a;
        while (my $rec = $select->fetchrow_hashref) {
                push @a, $rec->{n};
        }
        if ($select->rows) {
                my $format = join(" ", ("%3d") x @a) . "\n";
                printf $format, @a;
                $offset += $count;
        } else {
                $offset = 0;
        }
} while ($offset);


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to