An sql module that requires no knowledge of sql syntax.
Project description
SimpleSql
An sql module that requires no knowledge of sql syntax.
Installation
The simple sql module can be installed with pip
pip install pyssql
Demo
import pyssql
class User(
pyssql.Class
): # To create a class that can be stored in the database it must inherit simplesql.Class
user_count: int = 0 # Will not be restored when loading from database
# Sql types (these will be stored and loaded when storing to or loading from a database)
id: pyssql.Types.Integer(key=pyssql.Key.PRIMARY)
username: pyssql.Types.String(max_length=50, not_null=True)
def __init__(self, username: str) -> None:
self.id = User.user_count
self.username = username
User.user_count += 1
def on_load(
self,
) -> None: # Called when loading from database, this method is optional
print("Loading user", self.username, "with id", self.id)
def show(self) -> None:
print("ID:", self.id)
print("Username:", self.username)
# We'll create a database located in memory (by setting the path to ":memory:"), if you want to save it in a file and edit it later use a file path instead
database = pyssql.Database(":memory:", classes=[User])
# Populating the database
database.insert(User("John"))
database.insert(User("Mike"))
database.insert(User("Steve"))
database.insert(User("Jane"))
database.insert(User("Lisa"))
# Using the & operator is not recommended as it is very error prone
# users = database.select(User).where((User.id < 4) & (User.id > 2)).all()
# Instead chain the where(condition) method
users = (
database.select(User).where(User.id < 4).where(User.id > 2).all()
) # Same as User.id == 3
for user in users:
user.show()
john = database.select(User).where(User.username == "John").first()
john.show()
# When loading objects from the database, all their values will be converted to python types
print(type(john.id)) # <class 'int'>
Downloads
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 Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
pyssql-1.0.1-py3.9.egg
(11.6 kB
view details)
File details
Details for the file pyssql-1.0.1-py3.9.egg
.
File metadata
- Download URL: pyssql-1.0.1-py3.9.egg
- Upload date:
- Size: 11.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1a76dda6f3552af44785a2549ec8851a250e0ad5c7d1d2cf645c5406de57de1b |
|
MD5 | 138a1a185e89c7f7dc543039389d8181 |
|
BLAKE2b-256 | ad72c028203fc7fddfd4f877717835fe53b9d0e0e75af9bc9fdbd21539c4efe5 |