从tableView中的两个表中列出数据

时间:2014-07-08 11:24:10

标签: c++ qt sqlite qtgui

如何列出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);

1 个答案:

答案 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);