如何列出tableView中两个表的数据?
数据库(示例)Sqlite:
tb_sales
tb_product
tb_value
tb_customer_id (Customer Id "tb_customer")
tb_customer
tb_customer_id (id primary key)
tb_name
tb_state
QSqlRelationalTableModel
仅获取客户端的名称。还需要state
。
model= new QSqlRelationalTableModel(this);
model->setTable("tb_sales");
model->setRelation(2, QSqlRelation("tb_customer", "tb_customer_id", "tb_name"));
model->select();
ui->tableView->setModel(model);
答案 0 :(得分:1)
QSqlRelationalTableModel
仅允许您在辅助表中包含一列。您可以使用QSqlQueryModel
来查询加入两个表的查询:
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT tb_sales.* , tb_customer.tb_name, tb_customer.tb_state FROM tb_sales LEFT JOIN tb_customer ON tb_sales.tb_customer_id = tb_customer.tb_customer_id");
ui->tableView->setModel(model);