Module sqlite_shell
source code
Interactive SQLite shell.
This script provides an interactive shell interface to an SQLite database.
The user can enter an arbitrary SQL command (supported by SQLite) using a
GNU Readline interface (if available), and the shell displays table of results
in a reasonably formatted ASCII table (or in another specified format).
The intent is for SQLite to approach the same kind of easy-to-use interactive
tools present in a more elaborate MySQL setup (for example). To the author,
the nice advantage of SQLite is zero configuration to get the database
running: all you need is the single SQLite database file. With the inclusion
of SQLite in Python 2.5+, Python is all you need for access.
The intended use of this script is from a command-line shell, as in:
./sqlite_shell.py database_filename
or:
python sqlite_shell.py database_filename
You can also start the interactive shell from another Python program
by calling the shell function.
Although originally intended for purely ASCII interaction, it is often
helpful to have the results table of an SQL query in HTML format, for easy
cutting and pasting into the web (e.g., a blog or wiki). You can get
HTML-formatted tables using the -H or --html option.
To get a list of all command-line options; use sqlite_shell.py --help.
If you want to add support to shell for your own format, just update the
formats dictionary, or pass in your format function directly into shell.
A format function takes two arguments, a table represented by a list of tuples
where each tuple is a row, and a tuple of column descriptions (titles), and
returns a newline-terminated string representation of the table.
The formats dictionary maps string names of formats to format functions.
To add support for additional command-line options, you can modify the
global optparser object (an instance of optparse.OptionParser).
This software is written by Erik Demaine and distributed under the
MIT license:
Copyright (c) 2006-2007 Erik D. Demaine
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
|
|
formats = {'ascii': format_ascii, 'html': format_html,}
|
|
|
optparser = optparse.OptionParser(usage= '%prog [options] data...
|
|
ASCII formatting of tables.
This function lays out the table into an aligned ASCII table of all data,
with column titles at both the top and bottom, followed by a summary line
with the numbers of rows and columns. It computes the maximum width of
any entry in each column and uses this for layout. This can result in
very wide tables, in which case you hopefully have a nonwrapping terminal.
|
|
HTML formatting of tables.
This function renders a rudimentary HTML table, with column titles at
both the top and bottom. Column titles are set with headers <th> tags,
while data is set with standard <td> tags. The HTML is XML compatible
with explicit closing tags (</td>, </th>, and </tr>).
The table has a border=1 setting to make alignment clearer.
|
|
Interactive SQLite shell.
This function interactively executes SQL commands on the given SQLite
database db, specified as either an open SQLite connection or as a
string specifying the filename of a SQLite database to open.
You can also specify a format, either a string which refers to an
entry in the global formats dictionary, or a format function.
A format function takes two arguments, a table represented by a list of
tuples where each tuple is a row, and a tuple of column descriptions
(titles), and returns a newline-terminated string representation of the
table.
|
|
Main program driven by command-line options.
This function uses the global optparser object (an instance of
optparse.OptionParser) to parse arguments, so you can add additional
options to that object.
|
optparser
- Value:
optparse.OptionParser(usage= '%prog [options] database')
|
|