https://github.com/skyduy/dblink
安装:pip install dblink
假设数据库中有两张已存在的表users
和address
:
CREATE TABLE users (
id INTEGER NOT NULL,
name VARCHAR(50),
fullname VARCHAR(50),
password VARCHAR(12),
PRIMARY KEY (id)
);
CREATE TABLE addresses (
id INTEGER NOT NULL,
email_address VARCHAR NOT NULL,
user_id INTEGER,
PRIMARY KEY (id),
FOREIGN KEY(user_id) REFERENCES users (id)
);
你可以使用 Database Urls 进行连接数据库。
from dblink import Database, Table
# 手动关闭连接
db = Database(url='sqlite:///:memory:')
user_table = Table('users', db)
# 增删改查操作
db.close()
# 推荐使用 with 自动管理
with Database(url='postgresql://scott:tiger@localhost/mydatabase') as db:
address_table = Table('addresses', db)
# 增删改查操作
下面是使用例子
"""
Suppose you have two table: users and addresses.
"""
from dblink import Database, Table
with Database('sqlite:///:memory:') as db:
table_user = Table('users', db)
table_address = Table('addresses', db)
# show description
print(table_user.description)
print(table_address.description)
# chain query, you can call delete on the single table result
table_user.query.filter(id=1).one_or_none()
table_user.query.filter(id__gte=2) \
.order_by('name') \
.values_list('id', 'name')
table_user.query.filter(id__in=[1, 2, 3]) \
.filter(name__startswith='Yu').all()
table_user.query.order_by('-name') \
.values_list('fullname', flat=True, distinct=True)
table_user.query.distinct('name').values_list('name', flat=True)
table_user.query.filter(id__in=[1, 2, 3]).delete()
# join query
table_user.join(table_address) \
.filter(id__lt=10000) \
.filter(email_address__contains='gmail') \
.filter(**{'addresses.id__gte': 100}) \
.values_list('user_id', 'name', 'email_address',
table_address.id, 'users.fullname')
# get or insert
instance, create = table_user.get_or_insert(id=1, name='jone')
# single record operation.
table_user.insert({'id': 1, 'name': 'XiaoHong', 'password': 'psw'})
table_user.update({'id': 1, 'name': 'skyduy', 'password': 'psw'},
unique_fields=['id'], update_fields=['name', 'password'])
table_user.insert_or_update(
{'id': 1, 'name': 'skyduy', 'password': 'psw'},
unique_fields=['id'], update_fields=['name', 'password']
)
table_user.delete({'id': 1, 'name': "I don't matter"},
unique_fields=['id'])
# bulk operation
items = [{'id': 1, 'name': 'XiaoHong', 'password': 'haha'},
{'id': 2, 'name': 'skyduy', 'password': 'aha'},]
unique_fields = ['id']
update_fields = ['name']
table_user.bulk_insert(items)
table_user.bulk_delete(items, unique_fields)
table_user.bulk_update(items, unique_fields, update_fields)
table_user.bulk_insert_or_update(items, unique_fields, update_fields)
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.