![]() |
![]() |
Supporting the evolution of a new free world. |
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
MySQL DBE Driver
DOCUMENTATION
NAME
DBE::Driver::MYSQL - MySQL driver for the Perl Database Express Engine
SYNOPSIS
use DBE; $dsn = "Driver=MySQL;Host=localhost;User=root;Db=test;Charset=utf8"; $con = DBE->connect( $dsn ); $res = $con->query( 'SELECT * FROM table' ); $con->prepare( 'SELECT * FROM table' ); $res = $con->execute(); EXAMPLE
use DBE; # make a connection $con = DBE->connect( 'driver' => 'MySQL', 'socket' => '/tmp/mysql.sock', 'user' => 'db_user', 'password' => 'db_pass', 'db' => 'test', 'charset' => 'utf8', 'reconnect' => 1, ); # create a table $con->do( qq/ CREATE TABLE CFG_Config ( CFG_Index INT PRIMARY KEY auto_increment, CFG_Name VARCHAR(64), CFG_Value VARCHAR(64), UNIQUE (CFG_Name) ) / ); # insert an entry $con->do( "INSERT INTO CFG_Config (CFG_Name,CFG_Value) VALUES('foo','bar')" ); # or with placeholders # $con->do( # "INSERT INTO CFG_Config (CFG_Name,CFG_Value) VALUES(?,?)", # 'foo', 'bar' # ); # select the entry $res = $con->query( "SELECT CFG_Value FROM CFG_Config WHERE CFG_Name = 'foo'" ); # or with placeholders # $res = $con->query( # "SELECT CFG_Value FROM CFG_Config WHERE CFG_Name = ?", # 'foo' # ); ($foo) = $res->fetch_row(); print "\$foo = '$foo'\n"; # output: # $foo = 'bar' DESCRIPTION
DBE::Driver::MYSQL is a MySQL database driver for the Perl Database Express Engine.
Connection Parameters
use DBE; DBE->connect( 'Provider=MySQL;Database=test;User=root;Password=' ); Following parameters can be set to the connect() function:
Note: The connection attributes below can also be set with connection parameters.
Connection Attributes
Connection attributes can be set with connect() and attr_set(), and can be read with attr_get(). Following attributes are supported by the driver:
Large Objects
MySQL supports streaming of large objects inside statements. The following show you how to access large objects.
Reading from LOB and TEXT fields
When a BLOB or TEXT field exceeds LOB_MAX_SIZE and LOB_TRUNCATE is disabled it can be accessed through DBE`s Large Object class.
Note: Works inside statements only.
$stmt = $con->prepare( "SELECT lob FROM table" ); $res = $stmt->execute(); while( ($lob) = $res->fetchrow_array ) { if( ref( $lob ) ) { print "lob size is ", $lob->size, " bytes\n"; while( $got = $lob->read( $buf, 1024 ) ) { print "lob read $got bytes\n"; } $lob->close; # close the LOB handle } else { # LOB data can be accessed directly. print "lob size is ", length( $lob ), " bytes\n"; } } # close the statement $stmt->close; Writing to LOB and TEXT fields by using a LOB stream
Before creating a lob stream, you have to bind the parameter as string or binary type. You should not bind parameters after creating lob streams. The lob stream class is documented here.
$stmt = $con->prepare( 'INSERT INTO table (lob) VALUES(?)' ); # bind parameter 1 as binary type without data $stmt->bind_param( 1, '', 'b' ); # open a lob stream for parameter 1 $lob = $stmt->lob_open_write( 1 ); # write data to the lob $lob->write( 'writing data to the lob....' ); # close the lob stream $lob->close; # execute the statement $stmt->execute(); Writing to LOB and TEXT fields by reading from IO handles
$stmt = $con->prepare( 'INSERT INTO table (lob) VALUES(?)' ); # open a file open $fh, '< lob.file'; # bind a Perl IO handle to the lob parameter # use 'b' for binary data and 's' for text. $stmt->bind_param( 1, $fh, 'b' ); # execute the statement and write the contents of 'lob.file' to MySQL $stmt->execute(); # close the file close $fh; Writing to LOB and TEXT fields by reading from a class
This is a more complicated way, but will be used when setting a tied handle in the example above.
package MyInput; sub new { my $pkg = shift; my $this = { 'counter' => 0, 'max' => shift || 1, }; bless $this, $pkg; } # the class must implement a READ method, like a tied HANDLE does sub READ { my ($this, undef, $length) = @_; # read up to 'max' times $this->{'counter'} ++ == $this->{'max'} and return -1; # EOF # we need to write directly into the second parameter ($_[1]) $_[1] = 'TEST'; # was LOB_BUFFER_SIZE set to less then 4 bytes? $length < 4 and $_[1] = substr( $_[1], 0, $length ); # return the length of bytes read return length( $_[1] ); } package main; $stmt = $con->prepare( 'INSERT INTO table (lob) VALUES(?)' ); # make the class $in = MyInput->new( 3 ); # bind the class to parameter 1 $stmt->bind_param( 1, $in, 'b' ); # execute the statement and read from our class $stmt->execute(); # destroy the class undef $in; LIMITATIONS
Statements require MySQL 4.1 or higher.
KNOWN ERRORS
AUTHORS
Navalla org., Christian Mueller, http://www.navalla.org/
COPYRIGHT
The DBE::Driver::MYSQL module is free software. You may distribute under the terms of either the GNU General Public License or the Artistic License, as specified in the Perl README file.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Generated with Perl 5.10.1 and
Perl-CGI 1.0 over
FastCGI within 10.76ms
in memory safe mode.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||