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
, cout
ed 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
...
cout
ing 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[
] &&gave
what(): 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 push
ed_back
.