2023-07-18 21:33:40 +03:00
```
____ _ __
/ __ \__ __ (_)____/ /_____ _____
/ / / / / / / / ___/ //_/ _ \/ ___ /
/ /_/ / /_/ / / /__/ ,< / __ / /
\___\_\__,_/_/\___/_/|_|\___/_/
```
2023-07-09 04:03:24 +03:00
**Quicker** is a pythonic tool for querying databases.
2023-07-09 03:47:50 +03:00
2023-07-19 06:44:58 +03:00
Quicker wraps Python bindings on DBMS libraries:
2023-07-09 03:47:50 +03:00
- `mysqlclient` for MySQL.
2023-07-19 06:44:58 +03:00
- `psycopg2` for PostgreSQL.
- `sqlite` from Python standard library for SQLite (not implemented yet).
2023-07-09 03:47:50 +03:00
Connection parameters will passed to "backend" module as is.
2023-07-09 04:03:24 +03:00
# Installation
```
pip install git+https://git.nxhs.cloud/ge/quicker
```
2023-07-09 03:50:37 +03:00
# Usage
2023-07-09 03:47:50 +03:00
`Connection` is context manages and must be used with `with` keyword. `Connection` returns `Query` callable object. `Query` can be called in this ways:
```python
with Connection(**config) as db:
2023-07-19 06:44:58 +03:00
db.execute("sql query here...")
2023-07-09 04:16:46 +03:00
db.query("sql query here...") # query is alias for exec()
2023-07-09 03:47:50 +03:00
# Query is callable and you can also do this:
with Connection(**config) as query:
query("sql query here...")
```
2023-07-09 04:16:46 +03:00
`Query` cannot be called itself, you must use `Connection` to correctly initialise `Query` object. Available methods and properties:
2023-07-09 03:47:50 +03:00
2023-07-19 06:44:58 +03:00
- `query()` , `execute()` . Execute SQL. There is You can use here this syntax: `query('SELECT * FROM users WHERE id = %s', (15,))` .
2023-07-09 03:47:50 +03:00
- `commit()` . Write changes into database.
2023-07-19 06:44:58 +03:00
- `cursor` . Access cursor object directly.
- `connection` . Access connection object directly.
2023-07-09 03:47:50 +03:00
2023-07-09 04:16:46 +03:00
Full example:
2023-07-09 03:47:50 +03:00
```python
import json
from quicker import Connection
config = {
'provider': 'mysql',
'host': '127.0.0.1',
'port': 3306,
'user': 'myuser',
'database': 'mydb',
'password': 'example',
}
with Connection(**config) as query:
users = query("SELECT * FROM `users` ")
print(json.dumps(users, indent=4))
```
`users` will presented as list of dicts:
```python
[
{
'id': 1,
'name': 'test',
'email': 'noreply@localhost'
},
{
'id': 2,
'name': 'user1',
'email': 'my@example.com'
}
]
```
2023-07-09 04:16:46 +03:00
Changing database:
2023-07-09 03:50:37 +03:00
```python
2023-07-09 03:47:50 +03:00
from quicker import Connection
with Connection(provider='mysql', read_default_file='~/.my.cnf') as db:
2023-07-19 06:44:58 +03:00
db.query("INSERT INTO `users` VALUE (3, 'user2', 'user2@example.org')")
2023-07-09 03:47:50 +03:00
```
2023-07-19 06:44:58 +03:00
Quicker by default make commit after closing context. Set option `commit=False` to disable automatic commit.
For logging add following code:
```
import logging
logging.basicConfig(level=logging.DEBUG)
```
Direct access to Cursor object:
```
from quicker import Connection, make_list
# config declaration here...
with Connection(**config) as db:
db.cursor.execute('SELECT `id` , `name` , `email` FROM `users` WHERE `name` = %s', ('John',))
users = db.cursor.fetchall()
# Note: user is tuple! Convert it to list of dicts!
colnames = [desc[0] for desc in db.cursor.description]
users_list = make_list(colnames, users)
```