|
1 | 1 | # QueryBuilder python module
|
2 | 2 |
|
3 |
| - |
4 |
| - |
5 | 3 | [](https://github.com/co0lc0der/simple-query-builder-python/release)
|
6 | 4 | 
|
7 |
| - |
8 | 5 | [](https://github.com/co0lc0der/simple-query-builder-python/blob/main/LICENSE.md)
|
| 6 | + |
| 7 | + |
| 8 | + |
9 | 9 |
|
10 | 10 | This is a small easy-to-use module for working with a database. It provides some public methods to compose SQL queries and manipulate data. Each SQL query is prepared and safe. QueryBuilder fetches data to _list_ by default. At present time the component supports SQLite (file or memory).
|
11 | 11 |
|
@@ -46,6 +46,7 @@ pip install https://github.com/co0lc0der/simple-query-builder-python/archive/mai
|
46 | 46 | - `column(col_index)` executes SQL query and returns the needed column of result, `col_index` is `0` by default
|
47 | 47 | - `pluck(key_index, col_index)` executes SQL query and returns a list of tuples (the key (usually ID) and the needed column of result), `key_index` is `0` and `col_index` is `1` by default
|
48 | 48 | - `go()` this method is for non `SELECT` queries. it executes SQL query and returns nothing (but returns the last inserted row ID for `INSERT` method)
|
| 49 | +- `exists()` returns `True` if SQL query has a row |
49 | 50 | - `count()` prepares a query with SQL `COUNT(*)` function and executes it
|
50 | 51 | - `query(sql, params, fetch_type, col_index)` executes prepared `sql` with `params`, it can be used for custom queries
|
51 | 52 | - 'SQL' methods are presented in [Usage section](#usage-examples)
|
@@ -85,20 +86,43 @@ SELECT * FROM `users` WHERE (`id` > 1) AND (`group_id` = 2);
|
85 | 86 | - Select a row with a `LIKE` and `NOT LIKE` condition
|
86 | 87 | ```python
|
87 | 88 | results = qb.select('users').like(['name', '%John%']).all()
|
| 89 | +# or since 0.3.5 |
| 90 | +results = qb.select('users').like('name', '%John%').all() |
88 | 91 | # or
|
89 | 92 | results = qb.select('users').where([['name', 'LIKE', '%John%']]).all()
|
90 | 93 | ```
|
91 | 94 | ```sql
|
92 | 95 | SELECT * FROM `users` WHERE (`name` LIKE '%John%');
|
93 | 96 | ```
|
94 | 97 | ```python
|
95 |
| -results = qb.select('users').notLike(['name', '%John%']).all() |
| 98 | +results = qb.select('users').not_like(['name', '%John%']).all() |
| 99 | +# or since 0.3.5 |
| 100 | +results = qb.select('users').not_like('name', '%John%').all() |
96 | 101 | # or
|
97 | 102 | results = qb.select('users').where([['name', 'NOT LIKE', '%John%']]).all()
|
98 | 103 | ```
|
99 | 104 | ```sql
|
100 | 105 | SELECT * FROM `users` WHERE (`name` NOT LIKE '%John%');
|
101 | 106 | ```
|
| 107 | +- Select a row with a `IS NULL` and `IS NOT NULL` condition (since 0.3.5) |
| 108 | +```python |
| 109 | +results = qb.select('users').is_null('phone').all() |
| 110 | +# or |
| 111 | +results = qb.select('users').where([['phone', 'is null']]).all() |
| 112 | +``` |
| 113 | +```sql |
| 114 | +SELECT * FROM `users` WHERE (`phone` IS NULL); |
| 115 | +``` |
| 116 | +```python |
| 117 | +results = qb.select('customers').is_not_null('address').all() |
| 118 | +# or |
| 119 | +results = qb.select('customers').not_null('address').all() |
| 120 | +# or |
| 121 | +results = qb.select('customers').where([['address', 'is not null']]).all() |
| 122 | +``` |
| 123 | +```sql |
| 124 | +SELECT * FROM `customers` WHERE (`address` IS NOT NULL); |
| 125 | +``` |
102 | 126 | - Select rows with `OFFSET` and `LIMIT`
|
103 | 127 | ```python
|
104 | 128 | results = qb.select('posts')\
|
@@ -331,13 +355,17 @@ qb.delete('comments')\
|
331 | 355 | DELETE FROM `comments` WHERE `user_id` = 10;
|
332 | 356 | ```
|
333 | 357 | - Truncate a table
|
| 358 | + |
| 359 | +This method will be moved to another class |
334 | 360 | ```python
|
335 | 361 | qb.truncate('users').go()
|
336 | 362 | ```
|
337 | 363 | ```sql
|
338 | 364 | TRUNCATE TABLE `users`;
|
339 | 365 | ```
|
340 | 366 | - Drop a table
|
| 367 | + |
| 368 | +This method will be moved to another class |
341 | 369 | ```python
|
342 | 370 | qb.drop('temporary').go()
|
343 | 371 | ```
|
|
0 commit comments