2011-09-01 17:21:53 +00:00
|
|
|
Bottle-SQLite
|
2018-09-16 04:49:39 +00:00
|
|
|
=============
|
2011-09-01 17:21:53 +00:00
|
|
|
|
2018-09-16 04:49:39 +00:00
|
|
|
[![Build Status](https://travis-ci.org/alenmeister/bottle-sqlite.svg?branch=master)](https://travis-ci.org/alenmeister/bottle-sqlite)
|
2014-03-29 18:39:42 +00:00
|
|
|
|
2011-09-01 17:21:53 +00:00
|
|
|
SQLite is a self-contained SQL database engine that runs locally and does not
|
|
|
|
require any additional server software or setup. The sqlite3 module is part of the
|
|
|
|
Python standard library and already installed on most systems. It it very useful
|
|
|
|
for prototyping database-driven applications that are later ported to larger
|
|
|
|
databases such as PostgreSQL or MySQL.
|
|
|
|
|
|
|
|
This plugin simplifies the use of sqlite databases in your Bottle applications.
|
|
|
|
Once installed, all you have to do is to add a ``db`` keyword argument
|
|
|
|
(configurable) to route callbacks that need a database connection.
|
|
|
|
|
|
|
|
Installation
|
2018-09-16 04:49:39 +00:00
|
|
|
------------
|
2011-09-01 17:21:53 +00:00
|
|
|
|
2018-09-16 04:49:39 +00:00
|
|
|
Install with one of the following commands:
|
2011-09-01 17:21:53 +00:00
|
|
|
|
|
|
|
$ pip install bottle-sqlite
|
|
|
|
$ easy_install bottle-sqlite
|
|
|
|
|
2018-09-16 04:49:39 +00:00
|
|
|
or download the latest version from github:
|
2011-09-01 17:21:53 +00:00
|
|
|
|
2014-03-29 18:33:56 +00:00
|
|
|
$ git clone git://github.com/bottlepy/bottle-sqlite.git
|
|
|
|
$ cd bottle-sqlite
|
2011-09-01 17:21:53 +00:00
|
|
|
$ python setup.py install
|
|
|
|
|
|
|
|
Usage
|
2018-09-16 04:49:39 +00:00
|
|
|
-----
|
2011-09-01 17:21:53 +00:00
|
|
|
|
2018-09-16 04:49:39 +00:00
|
|
|
Once installed to an application, the plugin passes an open `sqlite3.Connection`
|
|
|
|
instance to all routes that require a ``db`` keyword argument:
|
2011-09-01 17:21:53 +00:00
|
|
|
|
|
|
|
import bottle
|
|
|
|
|
|
|
|
app = bottle.Bottle()
|
|
|
|
plugin = bottle.ext.sqlite.Plugin(dbfile='/tmp/test.db')
|
|
|
|
app.install(plugin)
|
|
|
|
|
|
|
|
@app.route('/show/:item')
|
|
|
|
def show(item, db):
|
|
|
|
row = db.execute('SELECT * from items where name=?', item).fetchone()
|
|
|
|
if row:
|
|
|
|
return template('showitem', page=row)
|
|
|
|
return HTTPError(404, "Page not found")
|
|
|
|
|
|
|
|
Routes that do not expect a ``db`` keyword argument are not affected.
|
|
|
|
|
2018-09-16 04:49:39 +00:00
|
|
|
The connection handle is configured so that `sqlite3.Row` objects can be
|
2011-09-01 17:21:53 +00:00
|
|
|
accessed both by index (like tuples) and case-insensitively by name. At the end of
|
|
|
|
the request cycle, outstanding transactions are committed and the connection is
|
|
|
|
closed automatically. If an error occurs, any changes to the database since the
|
|
|
|
last commit are rolled back to keep the database in a consistent state.
|
|
|
|
|
|
|
|
Configuration
|
2018-09-16 04:49:39 +00:00
|
|
|
-------------
|
2011-09-01 17:21:53 +00:00
|
|
|
|
|
|
|
The following configuration options exist for the plugin class:
|
|
|
|
|
2018-09-16 04:49:39 +00:00
|
|
|
* **dbfile**: Database filename (default: in-memory database)
|
|
|
|
* **keyword**: The keyword argument name that triggers the plugin (default: 'db')
|
|
|
|
* **autocommit**: Whether or not to commit outstanding transactions at the end of the request cycle (default: True)
|
|
|
|
* **dictrows**: Whether or not to support dict-like access to row objects (default: True)
|
2011-09-01 17:21:53 +00:00
|
|
|
|
2018-09-16 04:49:39 +00:00
|
|
|
You can override each of these values on a per-route basis:
|
2011-09-01 17:21:53 +00:00
|
|
|
|
|
|
|
@app.route('/cache/:item', sqlite={'dbfile': ':memory:'})
|
|
|
|
def cache(item, db):
|
|
|
|
...
|
|
|
|
|
2018-09-16 04:49:39 +00:00
|
|
|
or install two plugins with different ``keyword`` settings to the same application:
|
2011-09-01 17:21:53 +00:00
|
|
|
|
|
|
|
app = bottle.Bottle()
|
|
|
|
test_db = bottle.ext.sqlite.Plugin(dbfile='/tmp/test.db')
|
|
|
|
cache_db = bottle.ext.sqlite.Plugin(dbfile=':memory:', keyword='cache')
|
|
|
|
app.install(test_db)
|
|
|
|
app.install(cache_db)
|
|
|
|
|
|
|
|
@app.route('/show/:item')
|
|
|
|
def show(item, db):
|
|
|
|
...
|
|
|
|
|
|
|
|
@app.route('/cache/:item')
|
|
|
|
def cache(item, cache):
|
|
|
|
...
|