CrystalDB is a simple and small ORM and no need to provide a model.
Project description
CrystalDB
CrystalDB is a simple and small ORM. It has few concepts, making it easy to learn and intuitive to use.
- a small, expressive ORM, and no need to provide a model, so it will not be difficult for the problem of sub-library or sub-tables.
- python3.
- need mysql-client or pymysql or mysql.connector.
- currently only supports mysql.
Installation
From PyPi:
$ pip install crysyaldb
Basic Usage
-
Connect to the database:
import crystaldb db_host = '127.0.0.1' db_port = 3306 db_user = 'root' db_pass = '123' db_database = 'testdb' db_handle = crystaldb.database( dbn='mysql', host=db_host, port=db_port, user=db_user, passwd=db_pass, db=db_database, debug=True)
-
Create table: (Temporarily not supported, need to be completed by yourself)
for example:
CREATE TABLE `user` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `gender` varchar(16) DEFAULT NULL, `name` varchar(16) DEFAULT NULL, `birthday` varchar(16) DEFAULT NULL, `age` int(11) unsigned DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-
Create row:
values = { 'gender': 'girl', 'name': 'xiaoli_orm', 'birthday': '1982-08-02', 'age': 36 } result = db_handle.operator("user").insert(**values) print(result) # ==> 1 # If debug is True, the following log will be printed, time unit ms. # 0.3162 (1): INSERT INTO user (age, birthday, gender, name) VALUES (36, '1982-08-02', 'girl', 'xiaoli_orm')
-
Querying:
result = db_handle.select("user", ["name", "age"]).filter( age=36, gender="girl").query() print(result.__len__()) # count print(result) # <crystaldb.utils.IterBetter object at 0x1115246a0> for item in result: print(item) # <Storage {'name': 'xiaowang', 'age': 36}> print(item.name) # xiaowang # If debug is True, the following log will be printed, time unit ms. # 0.8579 (5): SELECT user.name, user.age FROM user WHERE user.age = 36 AND user.gender = 'girl'
-
Update row:
where = dict(name="xiaoli_orm") values = {'gender': 'boy', 'birthday': '1981-08-02', 'age': 37} result = db_handle.operator("user").update(where, **values) print(result) # 1 # If debug is True, the following log will be printed, time unit ms. # 0.4399 (1): UPDATE user SET age = 37, birthday = '1981-08-02', gender = 'boy' WHERE name = 'xiaoli_orm';
-
Delete row:
where = dict(name="xiaoyu", age=27) result = db_handle.operator("user").delete(where) # If debug is True, the following log will be printed, time unit ms. # 0.3782 (1): DELETE FROM user WHERE age = 27 AND name = 'xiaoyu';
-
Get Debug Queries:
print(db_handle.get_debug_queries_info) # {'run_time': '0.8247', 'sql': "SELECT user.name, user.age FROM user WHERE user.age = 36 AND user.gender = 'girl'"}
Documentation
Learning more
Check the documentation for more examples or read test cases in unit test code.
Change Log
Discussing
- submit issue
- email: fuzctc@gmail.com
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
crystaldb-1.1.2.tar.gz
(23.4 kB
view hashes)
Built Distribution
crystaldb-1.1.2-py3-none-any.whl
(20.9 kB
view hashes)
Close
Hashes for crystaldb-1.1.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6f3de6ef78645c2ecca2265ee5c11d3cb597af6c99cacb538221237bb1d81ee0 |
|
MD5 | 49346e44dc4228a9b695202bc8a0b289 |
|
BLAKE2b-256 | 9df1011d72a5b49e02ca6c933f3ec98129ae271b4f385d1fe00bf455b95530c7 |