mySQl选择查询不显示第一行数据

时间:2016-04-28 23:25:55

标签: c++ mysql

我创建的mySQL查询不显示第一行数据,但其余的都没问题。这是链接mySQL数据库的C ++程序的一部分。

这是我的代码:

case 1:
                     cout << " You have selected the option 1" << endl;


                     MYSQL_RES *res_set1;                            //Create a pointer to receive the return value

                     MYSQL_ROW row1;                                //Assign variable for rows

                     sqlQuery1 = "select m.Name, ms.BoatName, o.LastName, o.FirstName, o.Address, o.City, o.State, o.Zip, ms.SlipNum from Marina m, MarinaSlip ms, Owner o where m.MarinaNum = ms.MarinaNum and ms.OwnerNum = o.OwnerNum";

                     mysql_query(connect, sqlQuery1.c_str());        //Send a query to the database

                     res_set1 = mysql_store_result(connect);         //Receive the result and store it in res_set


                     //Check whether the result is empty
                     if ((row1 = mysql_fetch_row(res_set1)) == NULL)
                     {
                        cout << " There are no records in the Database.";
                        cout << endl;
                        goto menuOptions;
                     }
                     else
                     {
                        cout << endl;
                        cout << "|" << setfill(' ') << setw(21) << left << "Marina Name" << "|" << setw(16) << left << "Boat Name" << "|" << setw(17) << left << "Last Name" << "|" << setw(18) << left << "First Name" << "|" << setw(21) << left << "Address" << "|" << setw(14) << left << "City" << "|" << setw(11) << left << "State" << "|" << setw(9) << left << "Zip" << "|" << setw(9) << left << "SlipNum" << endl;

                        cout << "+" << setfill('-') << setw(19) << left << "-" << "+" << setw(16) << left << "-" << "+" << setw(15) << left << "-" << "+" << setw(18) << left << "-" << "+" << setw(20) << left << "-" << "+" << setw(15) << left << "-" << "+" << setw(12) << left << "-"  << "+" << setw(10) << left << "-" << "+" << setw(10) << left << "-" << endl; 

                        //This while is to print all rows
                        while ((row1 = mysql_fetch_row(res_set1)) != NULL)
                         {

                         cout << setfill(' ') << setw(22) << left << row1[0] << setw(17) << left << row1[1] << setw(17) << left << row1[2] << setw(18) << left << row1[3] << setw(21) << left << row1[4] << setw(19) << left << row1[5] << setw(12) << left << row1[6] <<setw(13) << left << row1[7] << setw(6) << left << row1[8] << endl; 
                         cout << endl; 

                         }
                     }

                     mysql_free_result(res_set1);
                     break;

我试图弄清楚但没有改变......我做错了什么?

由于

1 个答案:

答案 0 :(得分:0)

这一行:

if ((row1 = mysql_fetch_row(res_set1)) == NULL)

获取第一行。但你不打印出来。您只是用它来确定您的查询是否有任何结果。然后这一行:

while ((row1 = mysql_fetch_row(res_set1)) != NULL)

运行循环,打印出从第2行开始的所有行。