【问题标题】:Qt ComboBox : SQLite Merge Two TableQt ComboBox:SQLite 合并两个表
【发布时间】:2016-11-17 07:28:02
【问题描述】:

我需要一些帮助。这是我的普通组合框的代码,它将通过选择客户 ID 来显示客户信息。如何通过组合框上的客户 ID 显示其他表 (LOAN) 中的数据以及 CUSTOMER 表中的信息。

void LoanRequest::on_comboBox_custID_activated(const QString &arg1)
{    
    Login conn;

    if (!conn.connOpen())
    {
      qDebug()<<"Failed to open the database.";
      return;
    }

   conn.connOpen();
   QSqlQuery qry;

   if(qry.exec("SELECT * FROM CUSTOMER WHERE Cust_ID='"+arg1+"'"))        
   {
      //database table, my 2 table that i want to merge
      //LOAN : loanid, loan_type, custid,loan_status
      //CUSTOMER : custid, custname, custic,custaddress, custtelno

      while(qry.next())
      {
        ui->label_name->setText(qry.value(2).toString());
        ui->label_icno->setText(qry.value(3).toString());
        ui->label_telno->setText(qry.value(5).toString());
      }
  }
  else
  {
     QMessageBox::critical(this,tr("Error"),qry.lastError().text());
  }      
  conn.connClose();
}

【问题讨论】:

  • 你不知道如何从两个表中进行SELECT?或者您不知道如何在组合框中显示结果?
  • @demonplus 两者。我通常只使用一张桌子,所以我不知道如何使用两张

标签: c++ qt sqlite combobox


【解决方案1】:

试试这样的:

if(qry.exec("SELECT CUSTOMER.custname, \
            CUSTOMER.custic,CUSTOMER.custtelno,LOAN.loanid \ 
            FROM CUSTOMER JOIN LOAN WHERE CUSTOMER.custid = LOAN.custid \
            AND Cust_ID='"+arg1+"'"))        
{
  //database table, my 2 table that i want to merge
  //LOAN : loanid, loan_type, custid,loan_status
  //CUSTOMER : custid, custname, custic,custaddress, custtelno

  while(qry.next())
  {
    ui->label_name->setText(qry.value(2).toString());
    ui->label_icno->setText(qry.value(3).toString());
    ui->label_telno->setText(qry.value(5).toString());
  }
}

【讨论】:

  • 非常感谢!那么如何将结果显示到标签上,就像以前我使用索引一样,但现在我将使用 2 个不同的表?
  • 你的意思是查询的结果?索引 0 是 CUSTOMER.custname,索引 1 是 CUSTOMER.cust ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-03-19
  • 1970-01-01
  • 1970-01-01
  • 2021-02-26
  • 2012-02-23
相关资源
最近更新 更多