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.
I'm not even sure if it's an empty string... It would be really nice if you found that out. Do some debugging and tell us what actually is inrows. By the way, the info about data format is all in the mysql api doc: dev.mysql.com/doc/refman/5.1/en/mysql-fetch-row.html , dev.mysql.com/doc/refman/5.0/en/c-api-data-structures.html , so there is no need to guess what types the returned values have...rows.size()is after thewhileloop returns. Does this match with the number of results you are expecting from your query? Also have a look at the example in the first link I gave you. Try it on your query.row[0]printing fine is valuable. In that case, let's see the code that you use to printrows[i], and while you're at it, include the line where you declare and definerows.if(row[1] && row[1] != "-1")is WRONG WRONG WRONG!