Skip to Main Content
Programming the Perl DBI
book

Programming the Perl DBI

by Tim Bunce, Alligator Descartes
February 2000
Intermediate to advanced content levelIntermediate to advanced
364 pages
11h 47m
English
O'Reilly Media, Inc.
Content preview from Programming the Perl DBI

Inserting and Updating LONG/LOB Columns

Some databases let you insert into LONG/LOB columns using SQL statements with literal strings, like this:

INSERT INTO table_name (key_num, long_description) VALUES (42, '...')

Ignoring portability for the moment, that’s fine for simple short textual strings, but soon runs into problems for anything else. Firstly, most databases have a limit on the maximum length of an SQL statement, and it’s usually far shorter than the maximum length of a LONG/LOB column. Secondly, most databases have limits on which characters can be included in literal strings. The DBD driver’s quote() method will do its best, but it’s often not possible to put all possible binary data values into a string. Finally, coming back to portability, many databases are strict about data typing and just don’t let you assign literal strings to LONG/LOB columns.

So how do we avoid these problems? Here’s where placeholders come to our aid once again. We discussed placeholders in some detail in Chapter 5 so we’ll only cover LONG/LOB issues here.

To use placeholders, we’d implement the statement above using the DBI as:

use DBI qw(:sql_types);

$sth = $dbh->prepare( "
    INSERT INTO table_name (key_num, long_description) VALUES (?, ?)
" );
$sth->bind_param( 1, 42 );
$sth->bind_param( 2, $long_description, SQL_LONGVARCHAR);
$sth->execute();

Passing SQL_LONGVARCHAR as the optional TYPE parameter to bind_ param() gives the driver a strong hint that you’re binding a LONG/LOB type. Some drivers ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Advanced Perl Programming, 2nd Edition

Advanced Perl Programming, 2nd Edition

Simon Cozens
Perl One-Liners

Perl One-Liners

Peteris Krumins
Advanced Perl Programming

Advanced Perl Programming

Sriram Srinivasan
Beginning Perl

Beginning Perl

Curtis Ovid Poe

Publisher Resources

ISBN: 1565926994Supplemental ContentErrata Page