Skip to main content

Simplify your database access.

Project description

# DBQuery

Simplify your database access.

A comfortable database configuration and query wrapper for the Python DB-API.


## Example

Sample code for connecting to an existing SQLite database and
printing some rows from a table named world:

```Python
from dbquery import SQLiteDB

db = SQLiteDB('<MY DATABASE>')
get_hello = db.Select('SELECT hello FROM world WHERE id=?')
for i in (123, 456):
rows = get_hello(i)
row_0 = rows[0]
hello = row_0[0]
print(hello)
```

The example can be simplified by assuming that each SQL execution only returns
exactly one row:

```Python
from dbquery import SQLiteDB

db = SQLiteDB('<MY DATABASE>')
get_hello = db.SelectOne('SELECT hello FROM world WHERE id=?')
for i in (123, 456):
hello = get_hello(i)
print(hello)
```


### Set up a database for the example code


```
$ sqlite3 test.db
sqlite> CREATE TABLE world (id INTEGER, hello VARCHAR);
sqlite> INSERT INTO world VALUES (123, 'a'), (456, 'b');
```

With data you can use `test.db` as database in the above examples. Just be
sure you call python from the same directory as where the database file is.


## Supported databases

- SQLite
- PostgreSQL (requires the presence of [Psycopg2](http://initd.org/psycopg/)


# Documentation

The [Python DB-API](https://www.python.org/dev/peps/pep-0249/) specifies
connections and cursors for executing SQL. DBQuery is designed to hide this
complexity when it is not needed. Instead it provides a DB and a Query class
for executing SQL. The DB (or one of its sub classes) save the connection
information and provide access to the Query classes with use this to execute
the provided SQL.

This way a it is possible to handle SQL queries as callable functions:
```python
db = DB(configuration)
get_user = SelectOne(
"Select email, first_name FROM users WHERE user_id=?")
email, first_name = get_user(123)
```

What is more, if the connection to a database gets lost DBQuery can
automatically try to reconnect up to a specified count of retries:
```python
db = DB(configuration, retry=3) # retry 3 time to reconnect
```


## Configuration

The exact behavior depends on the actual DB implementation for a specific
database. In general all configuration parameters are passed to the DB
constructor. Usually a connection to the database will not be opened until the
first query is made


### SQLiteDB

`database, **kwds` parameters of the SQLiteDB constructor will be passed on
the the SQLite connect function.


## PostgreSQL

Accepts either the DSN string or configuration parameters for the Psqycopg2
connect function as keyword parameters.


## Query

Execute a SQL query without being interested in any result. It is the base
class for all other queries. Overwrite `_produce_return` if you are
interested in creating your own class that does something with the cursor that
executed the query.


### Manipulation

Use this to execute any INSERT, UPDATE and similar queries when the rowcount
of the cursor should be returned. It is possible to automatically check the
value of the row count by setting the rowcount parameter. If the resulting
row count does not match the provided one a ManipulationCheckError will be
raised.

This can be used to for example make sure that only one row was updated by a
query:
```python
update_user_name = db.Manipulation(
"UPDTAE users SET name=%s WHERE id=%s", rowcount=1)
with db: # start a new transaction, does not work with SQLiteDB!
update_user_name("new_name", 123) # does a roll back if rowcount != 1
```

### Select

Returns the result of `fetchall()` thus making it ideal for SELECT queries.


### SelectOne

Check that only one row is returned by the specified query. Returns `None`
otherwise. If the result row contains only one column then only that columns
value will be returned:
```python
get_first_name = db.SelectOne("SELECT first_name FROM users where id=?")
first_name = get_first_name(123)
```


### Transaction

The DB instance acts as a context manager for starting a connection on
entering the context and committing the queries in between in exit. If an
exception happens a `rollback` call will be made instead.

`SQLiteDB` does not implement this feature.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

DBQuery-0.3.0.dev2.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

DBQuery-0.3.0.dev2-py2.py3-none-any.whl (12.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file DBQuery-0.3.0.dev2.tar.gz.

File metadata

  • Download URL: DBQuery-0.3.0.dev2.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for DBQuery-0.3.0.dev2.tar.gz
Algorithm Hash digest
SHA256 bbff27932dfca4a36e64c8aa3f58860e6d6c14fa5e054ea3701a0b57fa5973f0
MD5 219f6ee1c77094ec61014cc46215aa30
BLAKE2b-256 04d46443733ce2de8e27c0db2916cfadfa10bca9f5c04d4a734c7c63e7a21772

See more details on using hashes here.

File details

Details for the file DBQuery-0.3.0.dev2-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for DBQuery-0.3.0.dev2-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 ea8ae8585b15e88610fc52d5246c4b33c32c3a9377ae8fc8441bc9a5c77a2ced
MD5 fa154441d856564d295191eb9e8c06d9
BLAKE2b-256 181f0d171aef65e7e6d950d99fea5f050dff19c9b348674fcb2f6c6f6768ed1c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page