A KnexJS like module for abstracting Python's SQLite
Project description
Knexpy
A query builder for SQLite3 based on Knexjs
Features
- Transactions
- Type Checking
- Bulk Insert
- JSON mapping
You can report bugs and discuss feature on the GitHub issues page.
Examples
Creating a Table
from Knexpy import Knex, Field
db = Knex("<Database File Name/Path>")
db.table(
"c",
[
Field.integer("field"),
],
not_exists=False,
)
db.table(
"t",
[
Field.varchar("field"), # Default size: 255
Field.varchar("field2"),
Field.varchar("field3"),
Field.foreign_key("field4", "c", "id"),
],
not_exists=False, # IF NOT EXISTS clause. Defaults to True
)
When creating a table the fields
id
,created_at
,modified_at
are automatically generated. Theid
field is a hash based on the information of the Row
Basic Select
from Knexpy import Knex
db = Knex("<Database File Name/Path>")
query = (
db.select("id", "field", "field2", ["field3", "test"],...) # List type on select acts as an alias
.from_("t")
.where("field", "=", "12345")
.order_by("id")
)
query.query() # Returns data as JSON
query.query(False) # Returns data as tuples
Select with Subquery
from Knexpy import Knex
db = Knex("<Database File Name/Path>")
query = (
db.select("id", "field", "field2", ["field3", "test"],...)
.from_("t")
.where("field", "=", "12345")
.where(
"field4",
"=",
db.subquery().select("id").from_("c").where("field", "=", 12345),
join_type="OR", # If attribute not present defaults to "AND".
)
.order_by("id")
)
query.query() # Returns data as JSON
Insert JSON Data
from Knexpy import Knex
db = Knex("<Database File Name/Path>", type_check=True) # type_check enables type checking (duh) when inserting/updating data
db.insert_json("<table>", {
"field": "1",
"field2": "2",
"field3": "3"
})
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Knexpy-0.1.0.tar.gz
(23.4 kB
view details)
File details
Details for the file Knexpy-0.1.0.tar.gz
.
File metadata
- Download URL: Knexpy-0.1.0.tar.gz
- Upload date:
- Size: 23.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.0 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.1
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b38b3590584fd1df3ef46b581f0a129827a18e5664f4853ea4e261727a5b9997 |
|
MD5 | 1ef3368d6eaefeed9c2217d4ce9acc18 |
|
BLAKE2b-256 | 6053019b9c549b107580ae707765855cde84c22bbfe1d7c5e6c38f6bf75222d2 |