FlagBox - a simple mysql orm for python3
Functional overview
- Model-Oriented operations
- Basic CRUD
- Operator query
- Automatic safety escape
- Deadlock retry
Installing
shell>pip install flagbox
Basic
from flagbox import Mysql
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()
jack = Worker(username='Jack', password='JackSoHandsome', salary=3999.2)
jack.insert()
mary = Worker()
mary.username = 'Mary'
mary.password = 'MarySoBeautiful'
mary.insert()
all_workers = Worker.select().all()
the_first_worker = Worker.select().first()
rich_workers = Worker.select(Worker.salary>=3000.0).all()
worker_jack = Worker.select(
((Worker.username == 'jack') & (Worker.password == 'JackSoHandsome')) | (Worker.salary=='3999.2')
).first()
the_richest_worker = Worker.select(orders=[-Worker.salary]).first()
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))
worker_jack.salary = 3000.0
worker_jack.update()
worker_jack.delete()
Else
Currently supported MySQL fields
FlagBox | Mysql |
---|
PrimaryKeyField | NO |
BooleanField | BOOLEAN |
IntField | INT |
BigIntField | BIGINT |
FloatField | FLOAT |
DoubleField | DOUBLE |
VarcharField | VARCHAR |
TextField | TEXT |
PrimaryKeyField must be defined as the primary key.A basic definition below
mydb = Mysql('localhost', 'root', 'your-passwd', 'your-database')
class ModelName(mydb.Model):
__table__ = 'mytable'
id = mydb.PrimaryKeyField()