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 inherits all events from Mojo::EventEmitter and can emit the following new ones.
$db->on(close => sub { my $db = shift; ... });
Emitted when the database connection gets closed while waiting for notifications.
$db->on(notification => sub { my ($db, $name, $payload) = @_; ... });
Emitted when a notification has been received.
Mojo::SQLite::Database implements the following attributes.
my $dbh = $db->dbh; $db = $db->dbh($dbh);
DBD::SQLite database handle used for all queries.
# Use DBI utility methods my $quoted = $db->dbh->quote_identifier('foo.bar');
This attribute is DEPRECATED.
my $class = $db->results_class; $db = $db->results_class('MyApp::Results');
Class to be used by "query", defaults to Mojo::SQLite::Results. Note that this class needs to have already been loaded before "query" is called.
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 $db = Mojo::SQLite::Database->new; my $db = Mojo::SQLite::Database->new(dbh => $dbh, sqlite => Mojo::SQLite->new); my $db = Mojo::SQLite::Database->new({dbh => $dbh, sqlite => Mojo::SQLite->new);
Construct a new Mojo::SQLite::Database object.
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 reused.
This method is DEPRECATED.
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'}); my $results = $db->query('select ? as foo', {json => {bar => 'baz'}});
Execute a blocking SQL statement and return a results object based on "results_class" with the query results. The DBD::SQLite statement handle will be automatically reused when it is not active anymore, to increase the performance of future queries. You can also append a callback for API compatibility with Mojo::Pg; the query is still executed in a blocking manner.
$db->query('insert into foo values (?, ?, ?)' => @values => sub { my ($db, $err, $results) = @_; ... }); Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
Hash reference arguments containing type and value elements will use the specified bind type for the parameter, using types from "DBI Constants" in DBI; see "Blobs" in DBD::SQLite and the subsequent section for more information.
type
value
Hash reference arguments containing a value named json will be encoded to JSON text with "to_json" in Mojo::JSON. To accomplish the reverse, you can use the method "expand" in Mojo::SQLite::Results to decode JSON text fields to Perl values with "from_json" in Mojo::JSON.
json
# "I ♥ SQLite!" $db->query('select ? as foo', {json => {bar => 'I ♥ SQLite!'}}) ->expand(json => 'foo')->hash->{foo}{bar};
my $tables = $db->tables;
Return table and view names for this database, that are visible to the current user and not internal, as an array reference. Names will be quoted and prefixed by a schema name of "main" for standard tables, "temp" for temporary tables, and the appropriate schema name for attached databases.
"main"
"temp"
# Names of all tables say for @{$db->tables};
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.