首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >嵌入C++的C++

嵌入C++的C++
EN

Stack Overflow用户
提问于 2017-04-20 02:51:11
回答 1查看 218关注 0票数 0

当涉及到在C++中使用C++时,我遇到了一个问题。

这可能是我忽略的小东西,但在我的第一个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]时,我确实会得到返回的行(我希望看到的行)。

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

代码语言:javascript
运行
复制
#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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-04-20 20:20:54

修复了问题,能够在查询中使用rowFlight[0]

代码语言:javascript
运行
复制
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

    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43509378

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档