Privex的Python数据库助手和包装器(小型ORM)
项目描述
Privex的Python数据库库
+===================================================+
| © 2019 Privex Inc. |
| https://www.privex.io |
+===================================================+
| |
| Privex's Python Database Library |
| License: X11 / MIT |
| |
| Originally Developed by Privex Inc. |
| Core Developer(s): |
| |
| (+) Chris (@someguy123) [Privex] |
| |
+===================================================+
Privex's Python Database Library - Database wrappers, query builders, and other useful DB-related classes/functions
Copyright (c) 2019 Privex Inc. ( https://www.privex.io )
README正在建设中。
使用pip安装
我们建议至少使用Python 3.6 - 我们不能保证与旧版本的兼容性。
pip3 install privex-db
基本用法
使用SqliteWrapper
和SqliteBuilder
(db.builder()
)的基本用法
from os.path import expanduser
from privex.db import SqliteWrapper
# Open or create the database file ~/.my_app/my_app.db
db = SqliteWrapper(expanduser("~/.my_app/my_app.db"))
# Create the table 'items' and insert some items
db.action("CREATE TABLE items (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);")
db.action("INSERT INTO items (name) VALUES (?);", ["Cardboard Box"])
db.action("INSERT INTO items (name) VALUES (?);", ["Orange"])
db.action("INSERT INTO items (name) VALUES (?);", ["Banana"])
db.action("INSERT INTO items (name) VALUES (?);", ["Stack of Paper"])
item = db.fetchone("SELECT * FROM items WHERE name = ?", ['Orange'])
print(item.id, '-', item.name)
# Output: 2 - Orange
q = db.builder('items')
# Privex QueryBuilder's support chaining similar to Django's ORM
q.select('id', 'name') \ # SELECT id, name FROM items
.where('name', 'Orange') \ # WHERE name = 'Orange'
.where_or('name', 'Banana') \ # OR name = 'Banana'
.order('name', 'id') # ORDER BY name, id DESC
# You can either iterate directly over the query builder object
for row in q:
print(f"ID: {row.id} Name: {row.name}")
# Output:
# ID: 3 Name: Banana
# ID: 2 Name: Orange
# Or you can use .fetch / .all to grab a single row, or all rows as a list
item = db.builder('items').where('name', 'Orange').fetch()
# {'id': 2, 'name': 'Orange'}
items = db.builder('items').all()
# [ {'id': 1, 'name': 'Cardboard Box'}, {'id': 2, 'name': 'Orange'}, ... ]
文档
此项目的完整文档在上文提供(点击Read The Docs图像),包括
- 如何安装应用程序及其依赖项
- 如何使用各种函数和类
- 供贡献者使用模块和类的通用文档
构建文档
pip3 install pipenv
git clone https://github.com/Privex/python-db
cd python-db/docs
pipenv install -d
# It's recommended to run make clean to ensure old HTML files are removed
# `make html` generates the .html and static files in docs/build for production
make clean && make html
# After the files are built, you can live develop the docs using `make live`
# then browse to http://127.0.0.1:8100/
# If you have issues with content not showing up correctly, try make clean && make html
# then run make live again.
make live
单元测试
要运行单元测试,克隆项目并创建一个包含PostgreSQL数据库详情的.env
文件(用于Postgres包装器+构建器测试)。
DB_USER=yourname
DB_NAME=privex_py_db
DB_BACKEND=postgresql
LOG_LEVEL=DEBUG
安装所有必需的依赖项
pip3 install pipenv
pipenv install -d
现在运行测试(-v以获得更详细的测试输出)
pipenv run pytest -rxXs -v
项目详情
下载文件
下载适用于您平台的文件。如果您不确定选择哪个,请了解有关安装包的更多信息。
源分布
privex_db-0.9.2.tar.gz (38.9 kB 查看哈希值)
构建分布
privex_db-0.9.2-py3-none-any.whl (47.3 kB 查看哈希值)
关闭
privex_db-0.9.2.tar.gz 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | b7cffa20d6b516c82d8d09dec33259ae198249aec3fe2559195cbfba0bf761d2 |
|
MD5 | 76fd7801594b84f34c2e64de871d8cca |
|
BLAKE2b-256 | 2cb93de33a074ca433a6e8f6f40af68de033c1149fbc657f3ddb9456c1081d35 |
关闭
privex_db-0.9.2-py3-none-any.whl 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 7158831693bd426f8c380cc015cb5771e79ec70564e9063dd27592aafb88b236 |
|
MD5 | c6f356d78164d6237824601e11769fea |
|
BLAKE2b-256 | 1ca5056cef9fbdff9c02bc0773040cd0efcbd376e8588a54fc05629c6a55c37f |