?$dbh->do(qq{INSERT INTO files (file, dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, md5sum, bz2size, disc, newfile) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )}, undef, $filename, $dev, $ino, (sprintf "%lo", $mode), $nlink, $ui +d, $gid, $rdev, $size, $atime, $mtime, $ctime, $digest, undef, undef, undef);
I discovered recently that most DBD's allow you to use named placeholders if you use the bind_param function. Even if it's less concise, I think this is a good idea for readability and maintainability.
So instead of doing this:
You can do this:$sth = $dbh->prepare('SELECT * FROM modules WHERE module LIKE ? AN +D code LIKE ?'); $rv = $sth->execute('%v%', '%x%');
Sure, it's wordier, but you'll never count question-marks again! Plus, you can edit your SQL later; moving the placeholders won't matter.$sth = $dbh->prepare('SELECT * FROM modules WHERE module LIKE :m A +ND code LIKE :c'); $sth->bind_param(':m', "%v%"); $sth->bind_param(':c', "%x%"); $rv = $sth->execute;
Caveat... DBD:Pg seems to only allow one-character placeholders. I don't know why.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: named placeholders in DBI
by vladb (Vicar) on Sep 22, 2003 at 16:39 UTC | |
by simonm (Vicar) on Sep 22, 2003 at 21:25 UTC | |
Re: named placeholders in DBI
by tantarbobus (Hermit) on Sep 22, 2003 at 22:21 UTC | |
Re: named placeholders in DBI
by mpeppler (Vicar) on Sep 22, 2003 at 17:40 UTC | |
Re: named placeholders in DBI
by simonm (Vicar) on Sep 22, 2003 at 21:28 UTC | |
Re: named placeholders in DBI
by Juerd (Abbot) on Sep 24, 2003 at 14:03 UTC | |
by Jenda (Abbot) on Sep 25, 2003 at 12:55 UTC |