Skip to main content

使用手册


如何安装?

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())

Release files for nice-sql 3.0.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 nice-sql 3.0.2
File Size Uploaded
nice-sql-3.0.2.tar.gz 7.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for nice-sql 3.0.2
File Interpreter ABI Platform
nice_sql-3.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 18.6 kB

Release files / nice-sql-3.0.2.tar.gz

Download URL nice-sql-3.0.2.tar.gz
Size 7.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7c5facc6acfcd93716a47e543f17df0b136efdce3b3b08577fff375ddf86f7fc
BLAKE2b-256 checksum
How to use checksums
49d22a2aeceeb8947613dbfcf9cca36f9df338541e6cdec7a273bb2019e8fddc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.1

Release files / nice_sql-3.0.2-py3-none-any.whl

Download URL nice_sql-3.0.2-py3-none-any.whl
Size 11.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1574276f89c104b3e63e59b2a2725b8bf8e02ef80a24b943a761a487ddb04975
BLAKE2b-256 checksum
How to use checksums
a0bf1f7f6c3e278981cf85232ae3289a1a622ae0ea85d360ad5e8da60d01a3ad
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.1

Release history Release notifications | RSS feed

This release

3.0.2 This release

2 release files

3.0.1

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.0

2 release files

0.0.7

2 release files

0.0.6

2 release files

0.0.5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

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