2017-04-20 19 views
0

C++でMySqlを使って作業することについては疑問がありました。C++にMySqlが組み込まれています

これはおそらく私が見落としているほど小さいかもしれませんが、私の最初のwhileループでは、rowFlight[0]はフライトナンバー(1-5の間の数字)を保持しています。私は私の2番目のクエリ内それを使用しようとしている

mysql_query(connection ,"SELECT firstName, lastName FROM passenger, manifest, flight WHERE passenger.passnum = manifest.passnum AND manifest.flightnum = flight.flightnum AND flight.flightnum = '*rowFlight[0]';");

は何も戻されています。 mysql_num_rowsを使用して返された行の数を表示すると、0が表示されますが、*rowFlight[0]を使用する代わりに(1〜5の間の)クエリに値をハードコードすると、私は見たいと思う)。

あるクエリの結果を別のクエリで使用できる正しい方法は何ですか?

#include <iostream> 
#include <iomanip> 
#include <mysql.h> 
using std::cout; using std::cerr; 
using std::setw; using std::endl; 

int main() { 
MYSQL *connection, mysql; 
connection = mysql_init(&mysql); //initialize instance 

connection = mysql_real_connect(connection, SERVER, USER, PASSWORD, DATABASE, 0, NULL, 0); 

if(connection) { //if connected successfully 
    MYSQL_RES *returnValFlight; //pointer to receive the return value 
    MYSQL_ROW rowFlight; //variable for rows 

    mysql_query(connection ,"SELECT * FROM flight;"); //Pull all the flights (flightnum, origination, destination, miles) 
    returnValFlight = mysql_store_result(connection); //returnVal is a temporary file for the results of the query, a cursor 

    MYSQL_RES *returnValPassenger; //pointer to receive the return value 
    MYSQL_ROW rowPassenger; //variable for rows 

    cout << endl 
     << "Flight Number:  Flight Origination:  Flight Destination:  Miles:" << endl 
     << "--------------------------------------------------------------------------" << endl; 

    while ((rowFlight = mysql_fetch_row(returnValFlight)) != NULL) { //while not end of the cursor  
     cout << rowFlight[0] << rowFlight[1] << rowFlight[2] << rowFlight[3] << endl; //print flight info 

     mysql_query(connection ,"SELECT firstName, lastName FROM passenger, manifest, flight WHERE passenger.passnum = manifest.passnum AND manifest.flightnum = flight.flightnum AND flight.flightnum = '*rowFlight[0]';"); //query 
     returnValPassenger = mysql_store_result(connection); //returnVal is a temporary file for the results of the query, a cursor 


     while ((rowPassenger = mysql_fetch_row(returnValPassenger)) != NULL) //while not end of the cursor  
      cout << rowPassenger[0] << " " << rowPassenger[1] << endl; //print passengers on that flight 
    } 

    cout << endl; 

    mysql_free_result(returnValPassenger); 
    mysql_free_result(returnValFlight); 
    mysql_close(connection); //close connection 
} 
else //connection failed 
    cerr << "Connection Failed!" << endl; 

return 0; 
} 

答えて

0

rowFlight[0]を使用できる問題を修正しました。

while ((rowFlight = mysql_fetch_row(returnValFlight)) != NULL) { //while not end of the cursor  
     cout << setw(7) << rowFlight[0] << setw(24) << rowFlight[1] << setw(22) << rowFlight[2] << setw(18) << rowFlight[3] << endl << endl; //print flight info (flightnum, origination, destination, miles)) 

     ostringstream os; 

     os << "SELECT firstName, lastName FROM passenger, manifest WHERE passenger.passnum = manifest.passnum AND manifest.flightnum =" << rowFlight[0]; //pull all passengers on that flight 

     mysql_query(connection, os.str().c_str()); 
     returnValPassenger = mysql_store_result(connection); //returnVal is a temporary file for the results of the query, a cursor 

     cout << setw(50) << "List Of Passengers On Flight Number " << rowFlight[0] << endl //current flight number 
      << setw(50) << "Total Passengers -> " << mysql_num_rows(returnValPassenger) << endl << endl; //how mnay passengers on that flight? 

     while ((rowPassenger = mysql_fetch_row(returnValPassenger)) != NULL) //while not end of the cursor  
      cout << setw(35) << rowPassenger[0] << " " << rowPassenger[1] << endl; //print passengers on that flight 

    } 
関連する問題