https://github.com/co0lc0der/simple-query-builder-python
A simple SQL Query Builder Python module
https://github.com/co0lc0der/simple-query-builder-python
database python python-oop python3 query-builder sql sqlite3
Last synced: 7 months ago
JSON representation
A simple SQL Query Builder Python module
- Host: GitHub
- URL: https://github.com/co0lc0der/simple-query-builder-python
- Owner: co0lc0der
- License: mit
- Created: 2022-09-07T07:01:25.000Z (about 3 years ago)
- Default Branch: main
- Last Pushed: 2025-02-09T15:36:15.000Z (9 months ago)
- Last Synced: 2025-03-24T01:25:24.804Z (8 months ago)
- Topics: database, python, python-oop, python3, query-builder, sql, sqlite3
- Language: Python
- Homepage: https://pypi.org/project/simple-query-builder/
- Size: 79.1 KB
- Stars: 12
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# QueryBuilder python module
[](https://github.com/co0lc0der/simple-query-builder-python/release)

[](https://github.com/co0lc0der/simple-query-builder-python/blob/main/LICENSE.md)



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 _dictionary_ by default. At present time the component supports SQLite (file or memory).
## Contributing
Bug reports and/or pull requests are welcome
## License
The module is available as open source under the terms of the [MIT license](https://github.com/co0lc0der/simple-query-builder-python/blob/main/LICENSE.md)
## Installation
Install the current version with [PyPI](https://pypi.org/project/simple-query-builder):
```bash
pip install simple-query-builder
```
Or from Github:
```bash
pip install https://github.com/co0lc0der/simple-query-builder-python/archive/main.zip
```
## How to use
### Import the module and init `QueryBuilder` with `Database()`
```python
from simple_query_builder import *
qb = QueryBuilder(DataBase(), 'my_db.db')
# or DB in memory
qb = QueryBuilder(DataBase(), ':memory:')
```
### Usage examples
#### Select all rows from a table
```python
results = qb.select('users').all()
```
Result query
```sql
SELECT * FROM `users`;
```
#### Select rows with two conditions
```python
results = qb.select('users').where([['id', '>', 1], 'and', ['group_id', '=', 2]]).all()
```
Result query
```sql
SELECT * FROM `users` WHERE (`id` > 1) AND (`group_id` = 2);
```
#### Update a row
```python
qb.update('users', {
'username': 'John Doe',
'status': 'new status'
})\
.where([['id', '=', 7]])\
.limit()\
.go()
```
Result query
```sql
UPDATE `users` SET `username` = 'John Doe', `status` = 'new status'
WHERE `id` = 7 LIMIT 1;
```
More examples you can find in [documentation](https://github.com/co0lc0der/simple-query-builder-python/blob/main/docs/index.md)
## ToDo
I'm going to add the next features into future versions
- write more unit testes
- add subqueries for QueryBuilder
- add `BETWEEN`
- add `WHERE EXISTS`
- add TableBuilder class (for beginning `CREATE TABLE`, move `qb.drop()` and `qb.truncate()` into it)
- add MySQL support
- add PostgreSQL support
- add `WITH`
- and probably something more