python使用dataset快速使用SQLite

目录

一、官网地址

二、安装

[三、 快速使用](#三、 快速使用)


一、官网地址

GitHub - pudo/dataset: Easy-to-use data handling for SQL data stores with support for implicit table creation, bulk loading, and transactions.

二、安装

python 复制代码
 pip install dataset
如果是mysql,则多安装一个依赖:pip install mysqlclient

三、 快速使用

python 复制代码
import dataset

if __name__ == '__main__':
    """
    先天支持sqlite
    如果是mysql,则多安装一个依赖:pip install mysqlclient
    """
    db = dataset.connect('sqlite:///mydatabase.db')
    # 建表,如果表,则dataset会自动创建。
    table = db['user']
    # 新增
    table.insert(dict(name="张三丰", age=18, country='China'))
    # 新增
    table.insert(dict(name='Jane Doe', age=37, country='France', gender='female'))
    # 修改数据
    table.update(dict(name='张三丰', age=34), ['name'])  # 根据name值过滤进行修改
    # 快速事务,显式使用事务参考官网
    with dataset.connect('sqlite:///mydatabase.db') as tx:
        tx['user'].insert(dict(name='John Doe', age=46, country='China'))
    # 所有表
    tables = db.tables
    # 表字段
    columns = table.columns
    # 总行数
    count = len(table)
    # 所有数据
    users = table.all()
    # 搜索
    users_china = table.users_in(country='China')
    # 获取特定数据
    one = table.find_one(name='John Doe')
    # 查找多个
    users_in = table.find(id=[1, 3, 7])
    # 比较查找
    elderly_users1 = table.find(age={'>=': 70})
    possible_customers = table.find(age={'between': [21, 80]})
    elderly_users2 = table.find(table.table.columns.age >= 70)

    # 自定义SQL
    result = db.query('SELECT country, COUNT(*) c FROM user GROUP BY country')
相关推荐
noravinsc9 分钟前
人大金仓数据库 与django结合
数据库·python·django
郭尘帅66611 分钟前
Vue3中实现轮播图
开发语言·前端·javascript
豌豆花下猫16 分钟前
Python 潮流周刊#102:微软裁员 Faster CPython 团队(摘要)
后端·python·ai
Thomas_YXQ41 分钟前
Unity3D Overdraw性能优化详解
开发语言·人工智能·性能优化·unity3d
lanbing1 小时前
PHP 与 面向对象编程(OOP)
开发语言·php·面向对象
yzx9910131 小时前
Gensim 是一个专为 Python 设计的开源库
开发语言·python·开源
麻雀无能为力1 小时前
python自学笔记2 数据类型
开发语言·笔记·python
Ndmzi1 小时前
matlab与python问题解析
python·matlab
懒大王爱吃狼1 小时前
怎么使用python进行PostgreSQL 数据库连接?
数据库·python·postgresql
猫猫村晨总1 小时前
网络爬虫学习之httpx的使用
爬虫·python·httpx