Mojo::SQLite::Database - Database
use Mojo::SQLite::Database; my $db = Mojo::SQLite::Database->new(sqlite => $sql, dbh => $dbh); $db->query('select * from foo') ->hashes->map(sub { $_->{bar} })->join("\n")->say;
Mojo::SQLite::Database is a container for DBD::SQLite database handles used by Mojo::SQLite.
Mojo::SQLite::Database implements the following attributes.
my $dbh = $db->dbh; $db = $db->dbh(DBI->new);
DBD::SQLite database handle used for all queries.
my $sql = $db->sqlite; $db = $db->sqlite(Mojo::SQLite->new);
Mojo::SQLite object this database belongs to.
Mojo::SQLite::Database inherits all methods from Mojo::Base and implements the following new ones.
my $tx = $db->begin; my $tx = $db->begin('exclusive');
Begin transaction and return Mojo::SQLite::Transaction object, which will automatically roll back the transaction unless "commit" in Mojo::SQLite::Transaction has been called before it is destroyed.
# Insert rows in a transaction eval { my $tx = $db->begin; $db->query('insert into frameworks values (?)', 'Catalyst'); $db->query('insert into frameworks values (?)', 'Mojolicious'); $tx->commit; }; say $@ if $@;
A transaction locking behavior of deferred, immediate, or exclusive may optionally be passed; the default in DBD::SQLite is currently immediate. See "Transaction and Database Locking" in DBD::SQLite for more details.
deferred
immediate
exclusive
$db->disconnect;
Disconnect "dbh" and prevent it from getting cached again.
my $bool = $db->ping;
Check database connection.
my $results = $db->query('select * from foo'); my $results = $db->query('insert into foo values (?, ?, ?)', @values); my $results = $db->query('select ? as img', {type => SQL_BLOB, value => slurp 'img.jpg'});
Execute a blocking statement and return a Mojo::SQLite::Results object with the results. The DBD::SQLite statement handle will be automatically reused when it is not active anymore, to increase the performance of future queries. Pass a hash reference containing type and value elements to specify the bind type of the parameter, using types from "DBI Constants" in DBI; see "Blobs" in DBD::SQLite and the subsequent section for more information. You can also append a callback for API compatibility with Mojo::Pg; the query is still executed in a blocking manner.
type
value
$db->query('insert into foo values (?, ?, ?)' => @values => sub { my ($db, $err, $results) = @_; ... });
Report any issues on the public bugtracker.
Dan Book, dbook@cpan.org
dbook@cpan.org
Copyright 2015, Dan Book.
This library is free software; you may redistribute it and/or modify it under the terms of the Artistic License version 2.0.
Mojo::SQLite
To install Mojo::SQLite, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Mojo::SQLite
CPAN shell
perl -MCPAN -e shell install Mojo::SQLite
For more information on module installation, please visit the detailed CPAN module installation guide.