Interaction with database in a simple way
Project description
pypadb
对 Mybatis 的拙劣模仿
目前只支持了 MySQL
使用了 pydantic。
install
pip install pypadb
Usage
select
from pydantic import BaseModel
from conf.db_configurer import DbConfigurer
from decorator.select import select
class User(BaseModel):
id: int
account: str
@select('select * from user', data_type=User)
def get_sth_many() -> list[User]:
pass
@select('select * from user where id = %(id)s', data_type=User)
def get_sth_one(id: int) -> User:
pass
if __name__ == '__main__':
# end() must be called
DbConfigurer() \
.set_host('localhost') \
.set_user('root') \
.set_password('123456') \
.set_database('test') \
.end()
print(get_sth_many())
# [User(...), User(...), ...]
print(get_sth_one(1))
# id=1 account='...'
另一种方法
from pydantic import BaseModel
from conf.db_configurer import db_configurer
from conf.table_configurer import tables
from utils.conditions import Like, Limit
from utils.enums import LikeEnum
class User(BaseModel):
id: int
account: str
class Stuff(BaseModel):
name: str
count: int
if __name__ == '__main__':
db_configurer \
.set_host('localhost') \
.set_user('root') \
.set_password('123456') \
.set_database('test') \
.end()
# init tables
tables.init_tables(user=User, stuff=Stuff)
print(
tables.user.select_many(limit=Limit(1))
)
# [User(id=1, account='123456')]
print(
tables.user.select_many(limit=Limit(0, 3))
)
# [User(id=1, account='123456'), User(id=2, account='123454563466'), User(id=3, account='12gsdfhs')]
print(
tables.user.select_like(likes=Like('account', '123', LikeEnum.R_Like))
)
# Like(column, value, like_mode)
# argument likes: Union[list[Like], Like]
# print result [User(id=1, account='123456'), User(id=2, account='123454563466')]
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
pypadb-0.0.10.tar.gz
(12.0 kB
view hashes)
Built Distribution
pypadb-0.0.10-py3-none-any.whl
(15.4 kB
view hashes)