<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">NAME

    Mojo::SQLite - A tiny Mojolicious wrapper for SQLite

SYNOPSIS

      use Mojo::SQLite;
    
      # Create a table
      my $sql = Mojo::SQLite-&gt;new-&gt;from_filename($filename);
      $sql-&gt;db-&gt;query('create table names (id integer primary key autoincrement, name text)');
    
      # Insert a few rows
      my $db = $sql-&gt;db;
      $db-&gt;query('insert into names (name) values (?)', 'Sara');
      $db-&gt;query('insert into names (name) values (?)', 'Stefan');
    
      # Insert more rows in a transaction
      eval {
        my $tx = $db-&gt;begin;
        $db-&gt;query('insert into names (name) values (?)', 'Baerbel');
        $db-&gt;query('insert into names (name) values (?)', 'Wolfgang');
        $tx-&gt;commit;
      };
      say $@ if $@;
    
      # Insert another row and return the generated id
      $db-&gt;query('insert into names (name) values (?)', 'Daniel');
      say $db-&gt;query('select last_insert_rowid() as id')-&gt;hash-&gt;{id};
    
      # Select one row at a time
      my $results = $db-&gt;query('select * from names');
      while (my $next = $results-&gt;hash) {
        say $next-&gt;{name};
      }
    
      # Select all rows
      $db-&gt;query('select * from names')
        -&gt;hashes-&gt;map(sub { $_-&gt;{name} })-&gt;join("\n")-&gt;say;

DESCRIPTION

    Mojo::SQLite is a tiny wrapper around DBD::SQLite that makes SQLite
    &lt;https://www.sqlite.org/&gt; a lot of fun to use with the Mojolicious
    &lt;https://mojolico.us&gt; real-time web framework.

    Database and statement handles are cached automatically, so they can be
    reused transparently to increase performance. And you can handle
    connection timeouts gracefully by holding on to them only for short
    amounts of time.

      use Mojolicious::Lite;
      use Mojo::SQLite;
    
      helper sqlite =&gt;
        sub { state $sql = Mojo::SQLite-&gt;new-&gt;from_filename(shift-&gt;config('sqlite_filename')) };
    
      get '/' =&gt; sub {
        my $c  = shift;
        my $db = $c-&gt;sqlite-&gt;db;
        $c-&gt;render(json =&gt; $db-&gt;query('select datetime("now","localtime") as time')-&gt;hash);
      };
    
      app-&gt;start;

    All I/O and queries are performed synchronously. However, the
    "Write-Ahead Log" journal is enabled for all connections, allowing
    multiple processes to read and write concurrently to the same database
    file (but only one can write at a time). See http://sqlite.org/wal.html
    for more information.

      # Performed concurrently
      my $pid = fork || die $!;
      say $sql-&gt;db-&gt;query('select datetime("now","localtime") as time')-&gt;hash-&gt;{time};
      exit unless $pid;

    All cached database handles will be reset automatically if a new
    process has been forked, this allows multiple processes to share the
    same Mojo::SQLite object safely.

    Any database errors will throw an exception as RaiseError is
    automatically enabled, so use eval or Try::Tiny to catch them. This
    makes transactions with "begin" in Mojo::SQLite::Database easy.

    While passing a file path of :memory: (or a custom "dsn" with
    mode=memory) will create a temporary database, in-memory databases
    cannot be shared between connections, so subsequent calls to "db" may
    return connections to completely different databases. For a temporary
    database that can be shared between connections and processes, pass a
    file path of :temp: to store the database in a temporary file (this is
    the default).

EVENTS

    Mojo::SQLite inherits all events from Mojo::EventEmitter and can emit
    the following new ones.

 connection

      $sql-&gt;on(connection =&gt; sub {
        my ($sql, $dbh) = @_;
        ...
      });

    Emitted when a new database connection has been established.

ATTRIBUTES

    Mojo::SQLite implements the following attributes.

 dsn

      my $dsn = $sql-&gt;dsn;
      $sql    = $sql-&gt;dsn('dbi:SQLite:uri=file:foo.db');

    Data source name, defaults to a dbi:SQLite:uri= followed by a URI to a
    temporary file.

 max_connections

      my $max = $sql-&gt;max_connections;
      $sql    = $sql-&gt;max_connections(3);

    Maximum number of idle database handles to cache for future use,
    defaults to 5.

 migrations

      my $migrations = $sql-&gt;migrations;
      $sql           = $sql-&gt;migrations(Mojo::SQLite::Migrations-&gt;new);

    Mojo::SQLite::Migrations object you can use to change your database
    schema more easily.

      # Load migrations from file and migrate to latest version
      $sql-&gt;migrations-&gt;from_file('/home/dbook/migrations.sql')-&gt;migrate;

 options

      my $options = $sql-&gt;options;
      $sql        = $sql-&gt;options({AutoCommit =&gt; 1, RaiseError =&gt; 1});

    Options for database handles, defaults to activating sqlite_unicode,
    AutoCommit, AutoInactiveDestroy as well as RaiseError and deactivating
    PrintError. Note that AutoCommit and RaiseError are considered
    mandatory, so deactivating them would be very dangerous. See
    "ATTRIBUTES COMMON TO ALL HANDLES" in DBI and "DRIVER PRIVATE
    ATTRIBUTES" in DBD::SQLite for more information on available options.

METHODS

    Mojo::SQLite inherits all methods from Mojo::EventEmitter and
    implements the following new ones.

 db

      my $db = $sql-&gt;db;

    Get Mojo::SQLite::Database object for a cached or newly established
    database connection. The DBD::SQLite database handle will be
    automatically cached again when that object is destroyed, so you can
    handle connection timeouts gracefully by holding on to it only for
    short amounts of time.

      # Add up all the money
      say $sql-&gt;db-&gt;query('select * from accounts')
        -&gt;hashes-&gt;reduce(sub { $a-&gt;{money} + $b-&gt;{money} });

 from_filename

      $sql = $sql-&gt;from_filename('C:\\Documents and Settings\\foo &amp; bar.db');

    Parse database filename directly. Unlike "from_string", the filename is
    parsed as a local filename and not a URI, and "options" must be
    specified separately.

      # Absolute filename
      $sql-&gt;from_filename('/home/fred/data.db');
    
      # Relative to current directory
      $sql-&gt;from_filename('data.db');
    
      # Temporary file database (default)
      $sql-&gt;from_filename(':temp:');
    
      # In-memory temporary database (single connection only)
      my $db = $sql-&gt;from_filename(':memory:')-&gt;db;
    
      # Additional options
      $sql-&gt;from_filename($filename)-&gt;tap(sub { $_-&gt;options-&gt;{PrintError} = 1 });

 from_string

      $sql = $sql-&gt;from_string('file:test.db');

    Parse configuration from connection string. Connection strings are
    parsed as URIs, so you should construct them using a module like
    Mojo::URL or URI::file. URI::file is recommended for portability on
    non-Unix-like systems. The scheme and hostname are optional, but if
    specified must be file and localhost respectively. If the connection
    string has a query string, it will be parsed and applied to "options".

      # Absolute filename
      $sql-&gt;from_string('file:///home/fred/data.db');
      $sql-&gt;from_string('file://localhost/home/fred/data.db');
      $sql-&gt;from_string('file:/home/fred/data.db');
      $sql-&gt;from_string('///home/fred/data.db');
      $sql-&gt;from_string('//localhost/home/fred/data.db');
      $sql-&gt;from_string('/home/fred/data.db');
    
      # Relative to current directory
      $sql-&gt;from_string('file:data.db');
      $sql-&gt;from_string('data.db');
    
      # Connection string must be a valid URI
      $sql-&gt;from_string(Mojo::URL-&gt;new-&gt;path($unix_filename));
      $sql-&gt;from_string(URI::file-&gt;new($filename));
    
      # Temporary file database (default)
      $sql-&gt;from_string(':temp:');
    
      # In-memory temporary database (single connection only)
      my $db = $sql-&gt;from_string(':memory:')-&gt;db;
    
      # Additional options
      $sql-&gt;from_string('data.db?PrintError=1&amp;sqlite_allow_multiple_statements=1');
      $sql-&gt;from_string(URI::file-&gt;new($filename)-&gt;Mojo::Base::tap(query_form_hash =&gt; {PrintError =&gt; 1}));

 new

      my $sql = Mojo::SQLite-&gt;new;
      my $sql = Mojo::SQLite-&gt;new('file:test.db');

    Construct a new Mojo::SQLite object and parse connection string with
    "from_string" if necessary.

      # Customize configuration further
      my $sql = Mojo::SQLite-&gt;new-&gt;dsn('dbi:SQLite:dbname=test.db');
      my $sql = Mojo::SQLite-&gt;new-&gt;dsn('dbi:SQLite:uri=file:test.db?mode=memory');
    
      # Pass filename directly
      my $sql = Mojo::SQLite-&gt;new-&gt;from_filename($filename);

REFERENCE

    This is the class hierarchy of the Mojo::SQLite distribution.

      * Mojo::SQLite

      * Mojo::SQLite::Database

      * Mojo::SQLite::Migrations

      * Mojo::SQLite::Results

      * Mojo::SQLite::Transaction

BUGS

    Report any issues on the public bugtracker.

AUTHOR

    Dan Book, dbook@cpan.org

COPYRIGHT AND LICENSE

    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.

SEE ALSO

    Mojolicious, DBD::SQLite

</pre></body></html>