2016-11-17 172 views
0

我需要一些幫助。這是我的正常組合框的代碼,它將通過選擇客戶ID來顯示客戶信息。我如何通過組合框上的客戶ID一起顯示來自CUSTOMER表的另一個表(LOAN)&的數據。Qt組合框:SQLite合併兩個表

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(); 
} 
+0

你不知道如何從兩個表中做出選擇?或者你不知道如何在combobox中顯示結果? – demonplus

+0

@demonplus兩者。我通常只使用一張桌子,所以我不知道如何使用兩張 – lawd

回答

0

嘗試這樣:

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()); 
    } 
} 
+0

非常感謝!那麼如何將結果顯示給標籤,因爲之前我使用了索引,但現在我將使用2個不同的表格? – lawd

+0

你是指查詢結果?索引零是CUSTOMER.custname,索引1是CUSTOMER.custic ... –