1

I'm trying to check for certain field values in an if. Most of these values are NULL in MySQL.

It seems to be screwing everything up.

I do

while ((row = mysql_fetch_row(res)) !=NULL){
    if(row[1] != "-1"){
        rows.push_back(row[0]);
    }
}

The field for row[1] is an INT LENGTH 1 equal to 1, -1, or NULL. Most are NULL, but a few are -1. I think that mysql.h outputs all values as char* (at least that's the only way I've been able to get it to work so far).

Anyways, strangely, rows gets filled, but it's filled with "nothing". I'm not even sure if it's an empty string or what.

Please help.

Many thanks in advance!


I put dashes in front and behind the std::cout of rows[i] in a for loop. It outputs a ton of --s.

If I std::cout the raw row[0], it outputs fine.


For us2012:

std::vector< char* > rows;

while ((row = mysql_fetch_row(res)) !=NULL){
    std::cout << "-" << row[0] << "-" << std::endl;
    if(row[1] != "-1"){
        rows.push_back(row[0]);
    }
}

std::cout << "Valids:" << std::endl;
for(int i = 0; i < rows.size(); ++i)
{
    std::cout << "id: -" << rows[i] << "-" << std::endl;
}

When I use this if

if(row[1] && row[1] != "-1")

The if works in reverse.

Note: I incremented an int in the while, couted that and rows and invalidRows. The counter gives 389, rows.size() 2, and invalidRows.size() 387.

I'm going to see if I can cout row[0] after I do the if...


couting row[0] after the if outputs the correct data.


if(row[1] != "-1") gives ...forbids comparison between pointer and integer...


Why mysql.h:

CentOS. I find hardly anything for it, and nothing my host supports.


if(row[1] && std::string(row[1]).compare("-1") != 0) put everything into invalidRows

Without row[] &&gavewhat(): basic_string::_S_construct NULL not valid Aborted`


Answer

It was if(row[1] && std::string(row[1]).compare("-1") == 0) all along! My logic got screwed up in all of the C++ crash coursing.

And using std::vector< std::string > allows row[x] to be pushed_back.

4

1 回答 1

2

First things first: The API you are using is a C API. There's nothing inherently wrong about that, but it means that you have to deal with a lot of stuff that seems unneccessary because C++ has better tools available for this. If possible, you should therefore use a C++ wrapper like MySQL++ : http://tangentsoft.net/mysql++/ (all credits for this suggestion go to @Non-Stop Time Travel in the comments below)


In your original code, you're not comparing strings, you're comparing addresses. Your condition compares the memory address stored in row[1] with the memory address of the constant string literal "-1". What you actually want (if you stick to char *) is strcmp : C++ Compare char array with string

You're also pushing back pointers to row[0] which is temporary, it is reassigned with every iteration of the while loop! You have to make a copy of these and store the pointers to the copies.

This is a very quick idea of how you could approach this. Not really good C++, though (in fact, if it were not for the vector and the cout, it may as well be C. See above for the reasons.):

#include <string.h>

std::vector<char*> rows;
while ((row = mysql_fetch_row(res)) !=NULL){
    if(strcmp(row[1],"-1") != 0){
        char * store = new char[strlen(row[0])+1];
        strncpy(store ,row[0] ,strlen(row[0])+1);
        rows.push_back(store);
    }
}
std::cout << "Valids:" << std::endl;
for(int i = 0; i < rows.size(); ++i)
{
    std::cout << "id: -" << rows[i] << "-" << std::endl;
}

//clean up
for(int i = 0; i < rows.size(); ++i)
{
    delete [] rows[i];
}

In general, when you deal with strings in C++ I would recommend using std::string and std::string::compare instead, but this is problematic here - if your mysql rows hold binary data, you don't have guaranteed null-terminated strings anymore (see the spec for MYSQL_ROW here: http://dev.mysql.com/doc/refman/5.0/en/c-api-data-structures.html ). As pointed out by Non-Stop Time Travel in the comments below, std::string supports internal \0s, the code example below however doesn't work with them.

#include <string>

//this particular code works only if row[0] and row[1] are nullterminated

std::vector<std::string> rows;
while ((row = mysql_fetch_row(res)) !=NULL){
    if(std::string(row[1]).compare("-1") != 0){
        rows.push_back(std::string(row[0]));
    }
}
std::cout << "Valids:" << std::endl;
for(int i = 0; i < rows.size(); ++i)
{
    std::cout << "id: -" << rows[i] << "-" << std::endl;
}

//cleaning up the vector of strings is not necessary! yay!

)

于 2013-01-24T02:01:42.510 回答