【问题标题】:Peewee - How do i execute raw query and map it to the dictionary?Peewee - 我如何执行原始查询并将其映射到字典?
【发布时间】:2019-05-20 19:54:26
【问题描述】:

我一直在尝试执行原始查询并将其映射到字典。

虽然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的结果

【问题讨论】:

  • 显示'show table status from ' + config.DB['name']然后你直接在数据库中看看你得到了什么。
  • @furas 我用查询中的示例字段更新了问题
  • 对我来说你不能使用Abc.raw,因为它会尝试将结果转换为Abc,但你的结果与Abc无关

标签: python mysql peewee


【解决方案1】:

也许有更好的方法,但使用 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) 

【讨论】:

  • 谢谢,我不知道cursor.description的存在
猜你喜欢
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
  • 2020-06-11
  • 2020-09-10
  • 2023-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多