Skip to main content

A lightweight SQLite helper library.

Project description

DBSqlite

基于 Sqlite3 的数据操控工具,对连接(connect)、游标(cursor)概念进行了封装,支持使用 Dict 数据类型来操作数据库。

支持事务。

简单示例

from DBSqlite import Sqlite

db = Sqlite('mydb.db')

rows = [{'name': 'xiaoming', 'age': '12', 'gender': 'male'},
        {'name': 'xiaohong', 'age': '11', 'gender': 'female'}]
ret = db.insert('table_student', rows)
print('ret:',ret)

# print
# ret:[{'id': 1, 'name': 'xiaoming', 'age': '12', 'gender': 'male', 'class': None}, {'id': 2, 'name': 'xiaohong', 'age': '11', 'gender': 'female', 'class': None}]

依赖

  • python3.6+
  • sqlite3: pip install pysqlite3

方法

DBSqlite 创建一个数据库工具实例

  • 必选参数

    • db:数据库文件路径
  • 返回:DBSqlit 示例

qj 执行一个查询语句,query json 的简写

  • 参数

    • sql 查询语句字符串
  • 返回:查询结果,数据结构为 list

qvs 执行一个查询语句, query values 的简写

  • 参数

    • sql 查询语句字符串
  • 返回:查询结果集的第一列组成的 list

qv 执行一个查询语句, query value 的简写

  • 参数

    • sql 查询语句字符串
  • 返回:查询结果集的第一行第一列的值

qo 执行一个查询语句 query object 的简写

  • 参数

    • sql 查询语句字符串
  • 返回:查询结果集的第一行的dict

de 执行一个sql语句 do execute 的简写

  • 参数

    • sql 查询语句字符串
  • 返回:影响行数

insert 向库表中插入多行记录

说明

  1. 以库表字段为主,记录中不提供视为None
  2. 每行记录的字段可以不一致
  3. 记录中不属于库表的字段会被忽略
  • 参数

    • table_name 库表名称
    • rows 需要插入的记录,list类型
  • 返回:插入库表的结果,list类型

update 更新库表中的多行记录

说明

  1. 只更新库表中存在且记录中提供的字段
  2. 每行记录的字段可以不一致
  3. 如果记录中没有提供主键,会被做插入处理
  • 参数

    • table_name 库表名称
    • rows 需要更新的记录,list类型
  • 返回:更新库表的结果,list类型

事务

"""测试事务功能"""
conn = db.get_conn()
try:
    # 插入数据
    db.de("INSERT INTO users (id, name, age) VALUES (4, 'David', 40);", conn=conn)

    # 查询事务中的数据
    result = db.qo("SELECT * FROM users WHERE id = 4", conn=conn)
    assert result["name"] == "David"

    # 模拟事务回滚
    raise Exception("模拟事务失败")
except Exception:
    conn.rollback()
finally:
    db.rls_conn(conn)

# 确保数据未提交
result = db.qo("SELECT * FROM users WHERE id = 4")
assert result is None

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

dbsqlite-1.0.1.tar.gz (6.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

DBSQLite-1.0.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file dbsqlite-1.0.1.tar.gz.

File metadata

  • Download URL: dbsqlite-1.0.1.tar.gz
  • Upload date:
  • Size: 6.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for dbsqlite-1.0.1.tar.gz
Algorithm Hash digest
SHA256 d750d28d1bb0afdb0d8852ad1dbc106bf6b83758f486dec0539cfc8ed9763c9d
MD5 557b802ce4ce36adfbdcffac5bffd145
BLAKE2b-256 1a413540158f637719bb556b457e546d46706e079b5e1694c9ab81ba1a9c7614

See more details on using hashes here.

File details

Details for the file DBSQLite-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: DBSQLite-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.3

File hashes

Hashes for DBSQLite-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 d2536ac30c53783f76f440a02b465f5ef79763f5ce7020b03938ec6ed580004e
MD5 8db548669403537cf2d6e6ca009b7bc3
BLAKE2b-256 f42871d2ebb1ee8af16156f3213281f3735dbd2558f04eb8f9ec546cb11e964f

See more details on using hashes here.

Supported by

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