A simple mysql orm for python3
Project description
中文-Chinese
pmorm.py - a simple mysql orm for python3
Functional overview
- Model-Oriented operations
- Basic CRUD
- Operator query
- Automatic safety escape
Installing
shell>pip install Pmorm
Basic
from pmorm import Mysql
# Create a connection.Automatically create database(if it is not created)
db = Mysql('localhost', 'root', 'your-password', 'testdb1')
class Worker(db.Model):
__table__ = 'worker'
id = db.PrimaryKeyField()
username = db.VarcharField(max_length=32, nullable=False, unique=True, default=None, comment="Worker's username")
password = db.VarcharField(max_length=32, nullable=False, unique=False, default=None, comment="Worker's password")
salary = db.FloatField(nullable=False, unique=False, default=0.0, comment="Worker's monthly salary")
Worker.create_table()
# Insert
jack = Worker(username='Jack', password='JackSoHandsome', salary=3999.2)
jack.insert()
mary = Worker()
mary.username = 'Mary'
mary.password = 'MarySoBeautiful'
mary.insert()
# Get all and get the first
all_workers = Worker.select().all()
the_first_worker = Worker.select().first()
# Query by operators
rich_workers = Worker.select(Worker.salary>=3000.0).all()
# Complex query by operators & and |
worker_jack = Worker.select(
((Worker.username == 'jack') & (Worker.password == 'JackSoHandsome')) | (Worker.salary=='3999.2')
).first()
# Order the rows
the_richest_worker = Worker.select(orders=[-Worker.salary]).first()
# Use the result
for worker in all_workers:
print('username:{} password:{} salary:{}'.format(worker.username, worker.password, worker.salary))
print('And the richest worker is {}'.format(the_richest_worker.username))
# Update one row
worker_jack.salary = 3000.0
worker_jack.update()
# Delete one row
worker_jack.delete()
Else
Currently supported MySQL fields
Pmorm | Mysql |
---|---|
PrimaryKeyField | NO |
BooleanField | BOOLEAN |
IntField | INT |
BigIntField | BIGINT |
FloatField | FLOAT |
DoubleField | DOUBLE |
VarcharField | VARCHAR |
TextField | TEXT |
PrimaryKeyField must be defined in each model, so a basic model looks like...
mydb = Mysql('localhost', 'root', 'your-passwd', 'your-database')
class ModelName(mydb.Model):
__table__ = 'mytable'
id = mydb.PrimaryKeyField()
# Other fields...
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Pmorm-0.21.tar.gz
(7.8 kB
view details)
Built Distribution
Pmorm-0.21-py3-none-any.whl
(7.7 kB
view details)
File details
Details for the file Pmorm-0.21.tar.gz
.
File metadata
- Download URL: Pmorm-0.21.tar.gz
- Upload date:
- Size: 7.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.9.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2f63f2201de3d191faf12749e7511ff5035cb95f7237ac66223dbbd1cf4ca5b0 |
|
MD5 | 67300fc4f87a3fecfaad357a16499899 |
|
BLAKE2b-256 | da84c493dbc11f1f42a2075f7f07bf6a6a9ba585ef4a5cd4c7e64df730b8f7ec |
File details
Details for the file Pmorm-0.21-py3-none-any.whl
.
File metadata
- Download URL: Pmorm-0.21-py3-none-any.whl
- Upload date:
- Size: 7.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.9.1 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.5.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 89994f2caa334c3d56570a189d8fbfc74450dff87a1567c5d36a8115541abfe6 |
|
MD5 | 455dfbc32e2b27dd8d2c91d7aa3660f2 |
|
BLAKE2b-256 | df708e94d114ff39273f03d039d16e50143f1a1377898fab73e1b1163db9fdc8 |