![](/img/trans.png)
[英]How do I access the last index of an ArrayField in a peewee query with postgres?
[英]Peewee - How do i execute raw query and map it to the dictionary?
我一直在嘗試執行原始查詢並將其映射到字典。
雖然execute_sql
不返回列名,但它返回元組。
我使用原始查詢,但它返回 None Abc 實例
class Abc(BaseModel):
name = CharField()
engine = CharField()
q = Abc.raw('show table status from ' + config.DB['name'])
print(list(q.execute()))
輸出:
[<Abc: None>, <Abc: None>, <Abc: None>, <Abc: None>, <Abc: None>, <Abc: None>, <Abc: None>, <Abc: None>, <Abc: None>, <Abc: None>, <Abc: None>, <Abc: None>, <Abc: None>, <Abc: None>, <Abc: None>]
來自 sql 的結果
也許有更好的方法,但是使用帶有列名的execute_sql
和cursor.description
我可以為所有表創建帶有字典的列表
import peewee
db = peewee.MySQLDatabase('my_database', user='my_user' password='my_password')
cursor = db.execute_sql('show table status from my_database')
all_tables = []
for row in cursor.fetchall():
table = dict()
for column, value in zip(cursor.description, row):
column_name = column[0]
print(column_name, '=', value)
table[column_name] = value
all_tables.append(table)
print(all_tables)
我的一個數據庫的結果:
[
{'Name': 'alembic_version', 'Engine': 'InnoDB', 'Version': 10, 'Row_format': 'Dynamic', 'Rows': 0, 'Avg_row_length': 0, 'Data_length': 16384, 'Max_data_length': 0, 'Index_length': 0, 'Data_free': 0, 'Auto_increment': None, 'Create_time': datetime.datetime(2019, 4, 29, 17, 19), 'Update_time': None, 'Check_time': None, 'Collation': 'latin1_swedish_ci', 'Checksum': None, 'Create_options': '', 'Comment': ''},
{'Name': 'users', 'Engine': 'InnoDB', 'Version': 10, 'Row_format': 'Dynamic', 'Rows': 0, 'Avg_row_length': 0, 'Data_length': 16384, 'Max_data_length': 0, 'Index_length': 65536, 'Data_free': 0, 'Auto_increment': 2, 'Create_time': datetime.datetime(2019, 4, 29, 17, 19), 'Update_time': None, 'Check_time': None, 'Collation': 'latin1_swedish_ci', 'Checksum': None, 'Create_options': '', 'Comment': ''},
{'Name': 'woocommerce', 'Engine': 'InnoDB', 'Version': 10, 'Row_format': 'Dynamic', 'Rows': 0, 'Avg_row_length': 0, 'Data_length': 16384, 'Max_data_length': 0, 'Index_length': 16384, 'Data_free': 0, 'Auto_increment': 3, 'Create_time': datetime.datetime(2019, 4, 29, 17, 19), 'Update_time': None, 'Check_time': None, 'Collation': 'latin1_swedish_ci', 'Checksum': None, 'Create_options': '', 'Comment': ''}
]
編輯:相同但具有列表理解
import peewee
db = peewee.MySQLDatabase('my_database', user='my_user' password='my_password')
cursor = db.execute_sql('show table status from my_database')
column_names = [x[0] for x in cursor.description]
all_tables = [dict(zip(column_names, row)) for row in cursor.fetchall()]
print(all_tables)
在 peewee 中, Model.raw()
用於模型上的手動優化select
語句,而不是一般的選擇語句 - 它返回模型的實例,其中聲明的字段是從選擇結果中填充的。 也就是說,如果您的Wiget(Model)
具有字段length
、 width
和height
,則查詢如下:
wigets = Wiget.raw('select length from wigets where width > height')
...將返回wigets
實例的可迭代Wiget
,其字段length
為返回值, width
和height
為None
,因為它們不在選擇列表中。 select * ...
將匹配所有返回到Wiget
字段的列並填寫它們。
如果查詢結果與模式模型不匹配(例如您的show table status ...
示例),它仍會為返回的每一行返回一個Wiget
實例,但無法將任何返回的列與您定義的模型匹配,所以它們都保留為None
。
重要說明,SQL 不區分大小寫,但 peewee 是,因此使用SELECT LENGTH WHERE ...
的查詢(如果初始create table
語句中的列為大寫,則select *
)不會填充length
字段,因為不同案例意味着它們不匹配。 也許這會在未來得到解決。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.