|
6 | 6 | [](https://github.com/seapagan/sqliter-py/actions/workflows/mypy.yml)
|
7 | 7 | <!--  -->
|
8 | 8 |
|
9 |
| -An SQLite wrapper in Python using Pydantic and written primarily using ChatGPT, |
10 |
| -as an experiment in how viable it is to write working code using a LLM. |
| 9 | +SQLiter is a lightweight Object-Relational Mapping (ORM) library for SQLite |
| 10 | +databases in Python. It provides a simplified interface for interacting with |
| 11 | +SQLite databases using Pydantic models. |
11 | 12 |
|
12 |
| -The code was then cleaned up, typed and linted by hand. |
| 13 | +It does not aim to be a full-fledged ORM like SQLAlchemy, but rather a simple |
| 14 | +and easy-to-use library for basic database operations, especially for small |
| 15 | +projects. It is NOT asynchronous and does not support complex queries (at this |
| 16 | +time). |
| 17 | + |
| 18 | +The ideal use case is more for Python CLI tools that need to store data in a |
| 19 | +database-like format without needing to learn SQL or use a full ORM. |
| 20 | + |
| 21 | +> [!NOTE] |
| 22 | +> This project is still in the early stages of development and is lacking some |
| 23 | +> planned functionality. Please use with caution. |
| 24 | +> |
| 25 | +> See the [TODO](TODO.md) for planned features and improvements. |
| 26 | +
|
| 27 | +## Features |
| 28 | + |
| 29 | +- Table creation based on Pydantic models |
| 30 | +- CRUD operations (Create, Read, Update, Delete) |
| 31 | +- Basic query building with filtering, ordering, and pagination |
| 32 | +- Transaction support |
| 33 | +- Custom exceptions for better error handling |
| 34 | + |
| 35 | +## Installation |
| 36 | + |
| 37 | +You can install SQLiter using pip: |
| 38 | + |
| 39 | +```bash |
| 40 | +pip install sqliter-py |
| 41 | +``` |
| 42 | + |
| 43 | +## Quick Start |
| 44 | + |
| 45 | +Here's a quick example of how to use SQLiter: |
| 46 | + |
| 47 | +```python |
| 48 | +from sqliter import SqliterDB |
| 49 | +from sqliter.model import BaseDBModel |
| 50 | + |
| 51 | +# Define your model |
| 52 | +class User(BaseDBModel): |
| 53 | + name: str |
| 54 | + age: int |
| 55 | + |
| 56 | + class Meta: |
| 57 | + table_name = "users" |
| 58 | + |
| 59 | +# Create a database connection |
| 60 | +db = SqliterDB("example.db", auto_commit=True) |
| 61 | + |
| 62 | +# Create the table |
| 63 | +db.create_table(User) |
| 64 | + |
| 65 | +# Insert a record |
| 66 | +user = User(name="John Doe", age=30) |
| 67 | +db.insert(user) |
| 68 | + |
| 69 | +# Query records |
| 70 | +results = db.select(User).filter(name="John Doe").fetch_all() |
| 71 | +for user in results: |
| 72 | + print(f"User: {user.name}, Age: {user.age}") |
| 73 | + |
| 74 | +# Update a record |
| 75 | +user.age = 31 |
| 76 | +db.update(user) |
| 77 | + |
| 78 | +# Delete a record |
| 79 | +db.delete(User, "John Doe") |
| 80 | +``` |
| 81 | + |
| 82 | +## Detailed Usage |
| 83 | + |
| 84 | +### Defining Models |
| 85 | + |
| 86 | +Models in SQLiter are based on Pydantic's `BaseModel`. You can define your |
| 87 | +models like this: |
| 88 | + |
| 89 | +```python |
| 90 | +from sqliter.model import BaseDBModel |
| 91 | + |
| 92 | +class User(BaseDBModel): |
| 93 | + name: str |
| 94 | + age: int |
| 95 | + email: str |
| 96 | + |
| 97 | + class Meta: |
| 98 | + table_name = "users" |
| 99 | + primary_key = "name" # Default is "id" |
| 100 | + create_id = False # Set to True to auto-create an ID field |
| 101 | +``` |
| 102 | + |
| 103 | +### Database Operations |
| 104 | + |
| 105 | +#### Creating a Connection |
| 106 | + |
| 107 | +```python |
| 108 | +from sqliter import SqliterDB |
| 109 | + |
| 110 | +db = SqliterDB("your_database.db", auto_commit=True) |
| 111 | +``` |
| 112 | + |
| 113 | +#### Creating Tables |
| 114 | + |
| 115 | +```python |
| 116 | +db.create_table(User) |
| 117 | +``` |
| 118 | + |
| 119 | +#### Inserting Records |
| 120 | + |
| 121 | +```python |
| 122 | +user = User( name="Jane Doe", age=25, email="[email protected]") |
| 123 | +db.insert(user) |
| 124 | +``` |
| 125 | + |
| 126 | +#### Querying Records |
| 127 | + |
| 128 | +```python |
| 129 | +# Fetch all users |
| 130 | +all_users = db.select(User).fetch_all() |
| 131 | + |
| 132 | +# Filter users |
| 133 | +young_users = db.select(User).filter(age=25).fetch_all() |
| 134 | + |
| 135 | +# Order users |
| 136 | +ordered_users = db.select(User).order("age DESC").fetch_all() |
| 137 | + |
| 138 | +# Limit and offset |
| 139 | +paginated_users = db.select(User).limit(10).offset(20).fetch_all() |
| 140 | +``` |
| 141 | + |
| 142 | +Note: The filtering in SQLiter is basic and supports exact matches. Complex |
| 143 | +queries like `age__lt` are not supported in the current implementation. |
| 144 | + |
| 145 | +#### Updating Records |
| 146 | + |
| 147 | +```python |
| 148 | +user.age = 26 |
| 149 | +db.update(user) |
| 150 | +``` |
| 151 | + |
| 152 | +#### Deleting Records |
| 153 | + |
| 154 | +```python |
| 155 | +db.delete(User, "Jane Doe") |
| 156 | +``` |
| 157 | + |
| 158 | +### Transactions |
| 159 | + |
| 160 | +SQLiter supports transactions using Python's context manager: |
| 161 | + |
| 162 | +```python |
| 163 | +with db: |
| 164 | + db.insert(User( name="Alice", age=30, email="[email protected]")) |
| 165 | + db.insert(User( name="Bob", age=35, email="[email protected]")) |
| 166 | + # If an exception occurs, the transaction will be rolled back |
| 167 | +``` |
| 168 | + |
| 169 | +## Contributing |
| 170 | + |
| 171 | +Contributions are welcome! Please feel free to submit a Pull Request. |
| 172 | + |
| 173 | +## License |
| 174 | + |
| 175 | +This project is licensed under the MIT License. |
| 176 | + |
| 177 | +## Acknowledgements |
| 178 | + |
| 179 | +SQLiter was initially developed as an experiment using ChatGPT, with subsequent |
| 180 | +manual refinements and improvements. |
0 commit comments