Skip to main content

image

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

Change Log

Discussing

Release files for crystaldb 1.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for crystaldb 1.1.2
File Size Uploaded
crystaldb-1.1.2.tar.gz 23.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for crystaldb 1.1.2
File Interpreter ABI Platform
crystaldb-1.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 44.4 kB

Release files / crystaldb-1.1.2.tar.gz

Download URL crystaldb-1.1.2.tar.gz
Size 23.4 kB
Tags Source
SHA-256 checksum
How to use checksums
dc5b37e7e933f84df736b05698b3594bcae9df90fbaf47210f0b6d8d7dc9f9e0
BLAKE2b-256 checksum
How to use checksums
6435dde99a53cb37b45e72f00f15d567d00a977c7dab8c8c12c7b9da822ecbe7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.7

Release files / crystaldb-1.1.2-py3-none-any.whl

Download URL crystaldb-1.1.2-py3-none-any.whl
Size 20.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6f3de6ef78645c2ecca2265ee5c11d3cb597af6c99cacb538221237bb1d81ee0
BLAKE2b-256 checksum
How to use checksums
9df1011d72a5b49e02ca6c933f3ec98129ae271b4f385d1fe00bf455b95530c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.20.0 setuptools/46.0.0 requests-toolbelt/0.9.1 tqdm/4.42.0 CPython/3.7.7

Release history Release notifications | RSS feed

This release

1.1.2 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.6

2 release files

1.0.3

1 release file

1.0.2

1 release file

1.0.1

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page