DBD-SQLite-Amalgamation

 view release on metacpan or  search on metacpan

lib/DBD/SQLite.pm  view on Meta::CPAN

  my $dbh = DBI->connect("dbi:SQLite:dbname=dbfile","","");

=head1 DESCRIPTION

SQLite is a public domain RDBMS database engine that you can find
at http://www.hwaci.com/sw/sqlite/.

Rather than ask you to install SQLite first, because SQLite is public
domain, DBD::SQLite includes the entire thing in the distribution. So
in order to get a fast transaction capable RDBMS working for your
perl project you simply have to install this module, and B<nothing>
else.

SQLite supports the following features:

=over 4

=item Implements a large subset of SQL92

See http://www.hwaci.com/sw/sqlite/lang.html for details.

=item A complete DB in a single disk file

Everything for your database is stored in a single disk file, making it
easier to move things around than with DBD::CSV.

=item Atomic commit and rollback

Yes, DBD::SQLite is small and light, but it supports full transactions!

=item Extensible

User-defined aggregate or regular functions can be registered with the
SQL parser.

=back

There's lots more to it, so please refer to the docs on the SQLite web
page, listed above, for SQL details. Also refer to L<DBI> for details
on how to use DBI itself.

=head1 CONFORMANCE WITH DBI SPECIFICATION

The API works like every DBI module does. Please see L<DBI> for more
details about core features.

Currently many statement attributes are not implemented or are
limited by the typeless nature of the SQLite database.

=head1 DRIVER PRIVATE ATTRIBUTES

=head2 Database Handle Attributes

=over 4

=item sqlite_version

Returns the version of the SQLite library which DBD::SQLite is using,
e.g., "2.8.0". Can only be read.

=item unicode

If set to a true value, DBD::SQLite will turn the UTF-8 flag on for all text
strings coming out of the database. For more details on the UTF-8 flag see
L<perlunicode>. The default is for the UTF-8 flag to be turned off.

Also note that due to some bizareness in SQLite's type system (see
http://www.sqlite.org/datatype3.html), if you want to retain
blob-style behavior for B<some> columns under C<< $dbh->{unicode} = 1
>> (say, to store images in the database), you have to state so
explicitely using the 3-argument form of L<DBI/bind_param> when doing
updates:

    use DBI qw(:sql_types);
    $dbh->{unicode} = 1;
    my $sth = $dbh->prepare
         ("INSERT INTO mytable (blobcolumn) VALUES (?)");
    $sth->bind_param(1, $binary_data, SQL_BLOB); # binary_data will
    # be stored as-is.

Defining the column type as BLOB in the DDL is B<not> sufficient.

=back

=head1 DRIVER PRIVATE METHODS

=head2 $dbh->func('last_insert_rowid')

This method returns the last inserted rowid. If you specify an INTEGER PRIMARY
KEY as the first column in your table, that is the column that is returned.
Otherwise, it is the hidden ROWID column. See the sqlite docs for details.

Note: You can now use $dbh->last_insert_id() if you have a recent version of
DBI.

=head2 $dbh->func( 'busy_timeout' )

Retrieve the current busy timeout.

=head2 $dbh->func( $ms, 'busy_timeout' )

Set the current busy timeout. The timeout is in milliseconds.

=head2 $dbh->func( $name, $argc, $func_ref, "create_function" )

This method will register a new function which will be useable in SQL
query. The method's parameters are:

=over

=item $name

The name of the function. This is the name of the function as it will
be used from SQL.

=item $argc

The number of arguments taken by the function. If this number is -1,
the function can take any number of arguments.

=item $func_ref

This should be a reference to the function's implementation.

=back

For example, here is how to define a now() function which returns the
current number of seconds since the epoch:

    $dbh->func( 'now', 0, sub { return time }, 'create_function' );

After this, it could be use from SQL as:

    INSERT INTO mytable ( now() );

lib/DBD/SQLite.pm  view on Meta::CPAN

        my $n = @$self;

        # Variance is NULL unless there is more than one row
        return undef unless $n || $n == 1;

        my $mu = 0;
        foreach my $v ( @$self ) {
            $mu += $v;
        }
        $mu /= $n;

        my $sigma = 0;
        foreach my $v ( @$self ) {
            $sigma += ($x - $mu)**2;
        }
        $sigma = $sigma / ($n - 1);

        return $sigma;
    }

    $dbh->func( "variance", 1, 'variance', "create_aggregate" );

The aggregate function can then be used as:

    SELECT group_name, variance(score) FROM results
    GROUP BY group_name;



=head2 $dbh->func( $name, $func_ref, 'create_collation' )

This method will register a new collation function which can then be used
from C<COLLATE> clauses within SQL. The method's parameters are:

=over

=item $name

The name of the collation; this is the name under which the
collation will be available from SQL.

=item $func_ref

This should be a reference to the collation's implementation.
The application defined collation routine should return negative, 
zero or positive if the first string is less than, equal to, or greater
than the second string. i.e. (STRING1 - STRING2).

=back

Collations C<binary> and C<nocase> are builtin within Sqlite.
Collations C<perl> and C<perllocale> are builtin within 
the C<DBD::SQLite> driver, and correspond to the 
Perl C<cmp> operator with or without the L<locale> pragma; 
so you can write for example 

  CREATE TABLE foo(txt1 COLLATE perl,
                   txt2 COLLATE perllocale,
                   txt3 COLLATE nocase)

If the attribute C<< $dbh->{unicode} >> is set, strings coming from
the database and passed to the collation function will be properly
tagged with the utf8 flag; but this only works if the 
C<unicode> attribute is set B<before> the call to 
C<create_collation>. The recommended way to activate unicode
is to set the parameter at connection time :

  my $dbh = DBI->connect("dbi:SQLite:dbname=foo", "", "", 
                          { RaiseError => 1,
                            unicode    => 1} );


=head2 $dbh->func( $n_opcodes, $handler, 'progress_handler' )

This method registers a handler to be invoked 
periodically during long running calls to SQLite.
An example use for this interface is to keep a GUI 
updated during a large query.
The parameters are:

=over

=item $n_opcodes

The progress handler is invoked once for every C<$n_opcodes>
virtual machine opcodes in SQLite.

=item $handler

Reference to the handler subroutine.  If the progress handler returns
non-zero, the SQLite operation is interrupted. This feature can be used to
implement a "Cancel" button on a GUI dialog box.

Set this argument to C<undef> if you want to unregister a previous
progress handler.

=back



=head1 BLOBS

As of version 1.11, blobs should "just work" in SQLite as text columns. However
this will cause the data to be treated as a string, so SQL statements such
as length(x) will return the length of the column as a NUL terminated string,
rather than the size of the blob in bytes. In order to store natively as a
BLOB use the following code:

  use DBI qw(:sql_types);
  my $dbh = DBI->connect("dbi:sqlite:/path/to/db");
  
  my $blob = `cat foo.jpg`;
  my $sth = $dbh->prepare("INSERT INTO mytable VALUES (1, ?)");
  $sth->bind_param(1, $blob, SQL_BLOB);
  $sth->execute();

And then retreival just works:

  $sth = $dbh->prepare("SELECT * FROM mytable WHERE id = 1");
  $sth->execute();
  my $row = $sth->fetch;
  my $blobo = $row->[1];
  
  # now $blobo == $blob

=head1 NOTES

To access the database from the command line, try using dbish which comes with
the DBI module. Just type:



( run in 2.820 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )