c++ - 嵌入 C++ 的 MySql

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

最佳答案

修复了问题,能够在查询中使用 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

    }

关于c++ - 嵌入 C++ 的 MySql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43509378/

相关文章:

c++ - 如何比较自定义类的 std::variant?

mysql - 在 MySQL 中永久设置 auto_increment_offset

mysql - 使用合并或 if 结果在 where 子句 mysql

mysql按字段分组,忽略特定列中的重复项

C++ 模板返回类型取决于模板参数?

c++ - Perl 系统调用导致核心转储但 $?保持为零

c++ - 开源网络摄像头库

mysql - 在两个不同的组中使用 ORDER BY 的 SQL 查询

mysql - 获取每第 N 行需要项目的 MySQL 结果

c++ - 应该回答什么 stod ("5.000e-M1")?