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;
}