Skip to main content

easy/lite/simple db query for python db api 2.0

Project description

使用手册


如何安装?

pip install nice-sql

如何使用?

Step 1: 添加 db 配置

from nicesql.db import add_db

if __name__ == '__main__':
    add_db("mysql://test:test@localhost:3306/test_db?charset=utf8mb4")

step2: 执行 sql

from nicesql.shortcut import select, update, insert, delete, sql


# 方式 1:直接查询
def way1():
    result = select("select * from t where a={a} and b in ({b})", a=1, b=["1", "2"]).execute()


# 方式 2:装饰器查询
@select("select * from t where a={a}")
def get(a=1):
    pass


def way2():
    return get(2)

扩展功能

  • 支持控制返回数据模型:
from nicesql.shortcut import select


class User:
    def __init__(self):
        self.id = None
        self.name = None


if __name__ == '__main__':
    # 使用 model(T) 方法设置数据模型
    users = select("select * from user where id in ({ids})", id=[1, 2, 3]).model(User).execute()
  • 支持 sql 中扩展列表:
/*
ids 对应的传参,可以是列表,会被展开成  v1,v2,v3...
但只会展开第一层,不支持递归展开
*/
select *
from t
where id in ({ids})
  • 支持返回第 1 条数据:
from nicesql.shortcut import select


class User:
    def __init__(self):
        self.id = None
        self.name = None


if __name__ == '__main__':
    # 使用 first(T=None) 控制只获取第一条数据, 同时也可以设置 model
    user = select("select * from user where id={id}", id=1).first(User).execute()
  • 支持设置多 DB
from nicesql.db import add_db

if __name__ == '__main__':
    # 通过设置别名参数,创建多个 db;
    add_db("mysql://username:password@localhost:3306/test1", "db1")
    add_db("mysql://username:passwprd@localhost:3306/test2", "db2")

    # 使用是通过 db(alias) 指定需要使用的 db 
    from nicesql.shortcut import insert

    insert("insert into t(name) values({name})", name="hello").db("db1").execute()
  • 占位符支持递归查询,同时支持管道进行数据处理
from nicesql.shortcut import select


class User:
    def __init__(self):
        self.id = 5


if __name__ == '__main__':
    select("select * from t where id={user.id|str}", user=User())

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

nice-sql-3.0.2.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

nice_sql-3.0.2-py3-none-any.whl (11.3 kB view details)

Uploaded Python 3

File details

Details for the file nice-sql-3.0.2.tar.gz.

File metadata

  • Download URL: nice-sql-3.0.2.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.1

File hashes

Hashes for nice-sql-3.0.2.tar.gz
Algorithm Hash digest
SHA256 7c5facc6acfcd93716a47e543f17df0b136efdce3b3b08577fff375ddf86f7fc
MD5 21fe306159a7a1bf2517e71961343f52
BLAKE2b-256 49d22a2aeceeb8947613dbfcf9cca36f9df338541e6cdec7a273bb2019e8fddc

See more details on using hashes here.

File details

Details for the file nice_sql-3.0.2-py3-none-any.whl.

File metadata

  • Download URL: nice_sql-3.0.2-py3-none-any.whl
  • Upload date:
  • Size: 11.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.10.1

File hashes

Hashes for nice_sql-3.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 1574276f89c104b3e63e59b2a2725b8bf8e02ef80a24b943a761a487ddb04975
MD5 72fa592442fd4eb76f82c76efe77a54b
BLAKE2b-256 a0bf1f7f6c3e278981cf85232ae3289a1a622ae0ea85d360ad5e8da60d01a3ad

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page