DBIx::Simple::Examples 1.26 review

Download
by rbytes.net on

License: Perl Artistic License
File size: 15K
Developer: Juerd Waalboer
0 stars award from rbytes.net

[COPRIGHT=1]

EXAMPLES

General

#!/usr/bin/perl -w
use strict;
use DBIx::Simple;

# Instant database with DBD::SQLite
my $db = DBIx::Simple->connect('dbi:SQLite:dbname=file.dat')
or die DBIx::Simple->error;

# Connecting to a MySQL database
my $db = DBIx::Simple->connect(
'DBI:mysql:database=test', # DBI source specification
'test', 'test', # Username and password
{ RaiseError => 1 } # Additional options
);

# Using an existing database handle
my $db = DBIx::Simple->connect($dbh);

# Abstracted example: $db->query($query, @variables)->what_you_want;

$db->commit or die $db->error;

Simple Queries

$db->query('DELETE FROM foo WHERE id = ?', $id) or die $db->error;

for (1..100) {
$db->query(
'INSERT INTO randomvalues VALUES (?, ?)',
int rand(10),
int rand(10)
) or die $db->error;
}

$db->query(
'INSERT INTO sometable VALUES (??)',
$first, $second, $third, $fourth, $fifth, $sixth
);
# (??) is expanded to (?, ?, ?, ?, ?, ?) automatically

Single row queries

my ($two) = $db->query('SELECT 1 + 1')->list;
my ($three, $four) = $db->query('SELECT 3, 2 + 2')->list;

my ($name, $email) = $db->query(
'SELECT name, email FROM people WHERE email = ? LIMIT 1',
$mail
)->list;

Or, more efficiently:

$db->query('SELECT 1 + 1')->into(my $two);
$db->query('SELECT 3, 2 + 2')->into(my ($three, $four));

$db->query(
'SELECT name, email FROM people WHERE email = ? LIMIT 1',
$mail
)->into(my ($name, $email));

Requirements:
Perl

DBIx::Simple::Examples 1.26 search tags