Skip to main content

database操作(支持 mysql sqlite)

使用教程

下载

pip install pydb3

创建连接

'''默认为sqlite'''
db = Db()
'''连接Sqlite'''
db = Db(file_path='D:\\db\\test.sqlite')
'''连接Mysql'''
db = Db('mysql', host='localhost', port='3306', user='root', database='db', password='123456')

创建表

from pydb3.db import Db
from pydb3.model import Integer, VarChar, Model, Image, Date, DateTime

'''定义模型'''


class Student(Model):
    name = VarChar(length=12, null=False, primary=False, comment="姓名")
    age = Integer(length=2, null=False, default=18, comment="年龄")
    score = Integer(length=11, null=False, default=10, comment="分数")
    mark = Integer(length=12, default=0, auto=True, null=False, primary=True, comment="标签")
    date = Date(length=12, default=0, null=False, auto=True, primary=False, comment='日期')
    image = Image(comment='图片')
    create_time = DateTime(default=None, null=False, auto=True, comment="时间")

    def __init__(self, name='test', age=None, mark=None, date=None, create_time=None, image=None):
        self.name = name
        self.age = age
        self.mark = mark
        self.date = date
        self.create_time = create_time
        self.image = image


if __name__ == '__main__':
    # 设置数据源
    db = Db()
    Student.set_db(db)
    s = Student()
    # 创建表
    s.create_table()
    # 获得所有学生
    students = Student.find()
    # 获得年龄为18岁的学生
    students = Student.find(age=18)
    # 保存到excel
    Student.export_excel('test.xlsx', students)
    # 从网页下载图片
    s.image = 'url=图片链接'
    # 从数据库中下载图片到本地
    s.image = 'path=保持路径'
    # 将对象相应属性保持对数据库
    s.save()

新增数据

db = Db()
datas = [None, '小明', 12]
'''相当于 insert into table values(NULL,'小明',12)'''
db.insert('table', datas).execute()

查询

db = Db()
'''相当于 select * from table where name=1 and age=1 limit 0,100'''
db.select("table").where(name=1).and_(age=1)[0:100].execute()
'''相当于 select * from table where name=1 and address like '%北京%'''
db.select("table").where(name=1, age=12).like(address='北京').execute()
'''相当于 select Count(*) from table'''
db.select('table').count().execute()
'''相当于 select name from table group by name having name='test' '''
db.select('table', 'name').group_by('name').having(name='test').execute()
'''相当于 select * from student join class on student.id=class.stu_id'''
db.select('student').join('class', id='stu_id').execute()

更新

db = Db()
'''相当于 update table  set name='李白' where name=1 and age=1 limit 0,100'''
db.update("table", name='李白').where(name=1).and_(age=1)[0:100].execute()

删除

db = Db()
'''相当于 delete from table where a=1'''
db.delete('table').where(a=1).execute()

Release files for pydb3 0.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pydb3 0.0.7
File Size Uploaded
pydb3-0.0.7.tar.gz 14.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pydb3 0.0.7
File Interpreter ABI Platform
pydb3-0.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 34.6 kB

Release files / pydb3-0.0.7.tar.gz

Download URL pydb3-0.0.7.tar.gz
Size 14.0 kB
Tags Source
SHA-256 checksum
How to use checksums
1c126338a4fe21a9efe4c8909639da15e5c95970220435de4bcbd9800419f5f4
BLAKE2b-256 checksum
How to use checksums
9db9cbc2f4727a63eefd8e21ad2dd57a60d50818260196c7f62598b7bcb53883
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release files / pydb3-0.0.7-py3-none-any.whl

Download URL pydb3-0.0.7-py3-none-any.whl
Size 20.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7b93703d08dae099bed8ccf656f1fa06b0dfdb093e0ffce4c5e635513f0e254b
BLAKE2b-256 checksum
How to use checksums
1b6277b0af085da92cb7c76a8fc65dd0ba4a3d4e020698dfc211a69776539dee
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release history Release notifications | RSS feed

This release

0.0.7 This release

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

0.0.1

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