Skip to main content

Fast Operation Database

Project description

说明

  • 从此告别 SQL 语句,直接调用方法就可以实现增删改查
  • python3.10+
  • 持续更新中...

更新历史

  • 2025/07/06
    • 一些优化
    • 新增SQL对象(快速构建 SQL 语句)
  • 2025/06/27
    • kwargs 中解析的True值为is not nullFalse值为is null
  • 2025/06/28
    • SQLResponse 统一存在三个属性

如何安装?

  • pip install fastdb

拿什么吸引你?

使用方式简单暴力,不用写SQL就能进行增删改查

连接方式

支持传统连接、URL连接

插入数据如此贴心

  • 自动推导

    • 传入dict是插入一条数据,传入list是插入多条数据
  • 多种插入模式

    • 模式1,插入时,数据冲突则报错
    • 模式2,插入时,数据冲突则忽略
    • 模式3,插入时,数据发生冲突,把数据进行更新操作

操练起来

连接 MySQL

数据库对象

from fastod import MySQL

# 方式1
db = MySQL(host="localhost", port=3306, username="root", password="root@0", db="test")  # 数据库对象

# 方式1
MYSQL_CONF = {
    'host': 'localhost',
    'port': 3306,
    'username': 'root',
    'password': '123456',
    'db': 'test'
}
db = MySQL(**MYSQL_CONF)  # 数据库对象

# 方式2
MYSQL_URL = "mysql://root:123456@localhost:3306/test"
db = MySQL.from_url(MYSQL_URL)  # 数据库对象

表格对象

student = db['student']
student = db.pick_table('student')

首先准备测试数据

# 一条龙服务,创建people表并插入测试数据,每次插入一千条,累计插入一万条
db.gen_test_table('people', once=1000, total=10000)
people = db['people']

插入数据

单条插入

data = {'id': 10001, 'name': '小明', 'age': 10, 'gender': '男'}

# 插入一条数据
people.insert_data(data)

# 当插入的数据与表中的数据存在冲突时(唯一索引值),直接调用会报错,如果补充<unique>参数,则不报错
people.insert_data(data, unique='id')

批量插入

data = [
    {'id': 10002, 'name': '小红', 'age': 12, 'gender': '女'},
    {'id': 10003, 'name': '小强', 'age': 13, 'gender': '男'},
    {'id': 10004, 'name': '小白', 'age': 14, 'gender': '男'}
]

# 插入多条数据
people.insert_data(data)

插入数据时,如果数据冲突则进行更新

data = {'id': 10001, 'name': '小明', 'age': 10, 'gender': '男'}

# 当数据冲突时,也可以直接进行更新操作,下面是把age更新为11
people.insert_data(data, update='age=age+1')

删除数据

# delete from people where id=1
people.delete(id=1)

# delete from people where id in (1, 2, 3)
people.delete(id=[1, 2, 3])

# delete from people where age=18 limit 100
people.delete(age=18, limit=100)

更新数据

# update people set name='tony', job='理发师' where id=1
people.update(new={'name': 'tony', 'job': '理发师'}, id=1)

# update people set job='程序员' where name='thomas' and phone='18959176772'
people.update(new={'job': '程序员'}, name='thomas', phone='18959176772')

查询数据

# select * from people where id=1
people.query(id=1)

# select name, age from people where id=2
people.query(pick='name, age', id=2)

# select * from people where age=18 and gender in ('男', '女')
people.query(age=18, gender=['男', '女'])

# select name from people where age=18 and gender in ('男', '女') limit 5
people.query(pick='name', age=18, gender=['男', '女'], limit=5)

随机数据

# 随机返回1条数据<dict>
print(people.random())

# 随机返回5条数据<list>
print(people.random(limit=5))

遍历表

# 遍历整张表,默认每轮扫描1000条,默认只打印数据
people.scan()


def show(lines):
    for some in enumerate(lines, start=1):
        print('第{}{}'.format(*some))


# 限制id范围为101~222,每轮扫描100条,每轮的回调函数为show
people.scan(sort_field='id', start=101, end=222, once=100, dealer=show)

# 限制id范围的基础上,限制age=18
people.scan(sort_field='id', start=101, end=222, once=100, dealer=show, add_cond='age=18')

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

fastod-0.2.5.tar.gz (12.6 kB view details)

Uploaded Source

Built Distribution

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

fastod-0.2.5-py3-none-any.whl (12.9 kB view details)

Uploaded Python 3

File details

Details for the file fastod-0.2.5.tar.gz.

File metadata

  • Download URL: fastod-0.2.5.tar.gz
  • Upload date:
  • Size: 12.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.5

File hashes

Hashes for fastod-0.2.5.tar.gz
Algorithm Hash digest
SHA256 823741ef8f5a6bf101b652d8bccf8a8a1caca95b9ec7a400c9bc860a783a0c41
MD5 bf547b4d67209e0d55b93edab02a8f54
BLAKE2b-256 7f843fbd2576e3942b3243cb61815ef461d67c02eb7547ad8c27ffd747f0107d

See more details on using hashes here.

File details

Details for the file fastod-0.2.5-py3-none-any.whl.

File metadata

  • Download URL: fastod-0.2.5-py3-none-any.whl
  • Upload date:
  • Size: 12.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.9.5

File hashes

Hashes for fastod-0.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 58c58899623bc483ecc9a1ba61875c735ca95eb49be786f11fc07a4672e11268
MD5 1c6ea98d9f50317c3e20d95180559505
BLAKE2b-256 4bfee5a05deb686b9704ad7d4a6ce6c1fdcc5f758a15b7d8b915522d61f10ea2

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