- Peewee 教程
- Peewee - 主頁
- Peewee - 概覽
- Peewee - 資料庫類
- Peewee - 模型
- Peewee - 欄位類
- Peewee - 插入一條新記錄
- Peewee - 選擇記錄
- Peewee - 過濾器
- Peewee - 主鍵和複合鍵
- Peewee - 更新現有記錄
- Peewee - 刪除記錄
- Peewee - 建立索引
- Peewee - 約束
- Peewee - 使用 MySQL
- Peewee - 使用 PostgreSQL
- Peewee - 動態定義資料庫
- Peewee - 連線管理
- Peewee - 關係和連線
- Peewee - 子查詢
- Peewee - 排序
- Peewee - 計數和彙總
- Peewee - SQL 函式
- Peewee - 檢索行元組/字典
- Peewee - 使用者定義運算子
- Peewee - 原子事務
- Peewee - 資料庫錯誤
- Peewee - 查詢生成器
- Peewee - 與 Web 框架整合
- Peewee - SQLite 擴充套件
- Peewee - PostgreSQL 和 MySQL 擴充套件
- Peewee - 使用 CockroachDB
- Peewee 有用資源
- Peewee - 快速指南
- Peewee - 有用資源
- Peewee - 討論
Peewee - 檢索行元組/字典
無需建立模型例項即可迭代結果集。這可以透過使用以下方式實現 -
tuples() 方法。
dicts() 方法。
示例
若要以元組集合形式返回選擇查詢中的欄位資料,請使用 tuples() 方法。
qry=Contacts.select(Contacts.City, fn.Count(Contacts.City).alias('count'))
.group_by(Contacts.City).tuples()
lst=[]
for q in qry:
lst.append(q)
print (lst)
輸出
輸出如下所示 -
[
('Chennai', 1),
('Delhi', 2),
('Indore', 1),
('Mumbai', 1),
('Nagpur', 1),
('Nasik', 3),
('Pune', 1)
]
示例
若要獲取字典物件集合 -
qs=Brand.select().join(Item).dicts() lst=[] for q in qs: lst.append(q) print (lst)
輸出
輸出如下所示 -
[
{'id': 1, 'brandname': 'Dell', 'item': 1},
{'id': 2, 'brandname': 'Epson', 'item': 2},
{'id': 3, 'brandname': 'HP', 'item': 1},
{'id': 4, 'brandname': 'iBall', 'item': 3},
{'id': 5, 'brandname': 'Sharp', 'item': 2}
]
廣告