簡體   English   中英

Peewee - 我如何執行原始查詢並將其映射到字典?

[英]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_sqlcursor.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)具有字段lengthwidthheight ,則查詢如下:

wigets = Wiget.raw('select length from wigets where width > height')

...將返回wigets實例的可迭代Wiget ,其字段length為返回值, widthheightNone ,因為它們不在選擇列表中。 select * ...將匹配所有返回到Wiget字段的列並填寫它們。

如果查詢結果與模式模型不匹配(例如您的show table status ...示例),它仍會為返回的每一行返回一個Wiget實例,但無法將任何返回的列與您定義的模型匹配,所以它們都保留為None

重要說明,SQL 不區分大小寫,但 peewee 是,因此使用SELECT LENGTH WHERE ...的查詢(如果初始create table語句中的列為大寫,則select * )不會填充length字段,因為不同案例意味着它們不匹配。 也許這會在未來得到解決。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM