2017-04-20 93 views
0

我在C++中使用MySql时遇到了一个问题。嵌入在C++中的MySql

这可能是我忽略的一些小东西,但在我的第一个while循环中,rowFlight[0]包含什么是flightnum(1-5之间的数字)。

我试图用我的第二个查询中,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,但是当我在查询中(1和5之间)硬编码一个值而不是使用*rowFlight[0]时,我确实得到了返回的行(行我期望看到)。

能够在另一个查询中使用一个查询结果的正确方法是什么?

#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 

    }