When I’m trying to fetch data from the sqlite database table the NSArray has a capacity of 100 and the count array has a capacity of 9. count[5] returns to me rubbish data which is not in the table at all. Even the first 5 records returned correctly.
if(sqlite3_open([dbPath UTF8String],&database)==SQLITE_OK){
const char* sql2= " select * from Bcars";
sqlite3_stmt *selectstatment;
if(sqlite3_prepare_v2(database,sql2,-1,&selectstatment,Nil)==SQLITE_OK){
while(sqlite3_step(selectstatment)==SQLITE_ROW){
// fetch the id
count[i++]=sqlite3_column_int(selectstatment, 0);
carobject.primarykey=sqlite3_column_int(selectstatment, 0);
[ar addObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(selectstatment, 1)]];
[ar1 addObject:[NSString stringWithUTF8String:(char*)sqlite3_column_text(selectstatment, 2)]];
}
}
}
else
{
sqlite3_close(database);
self.statustext.text=@" database closed";
}
self.statustext.text=[[NSString alloc]initWithFormat:@"%d",count[4]];/* when I try to return count[5] it gives me rabbish value !!*/
self.searchtext.text=(NSString*)[ar objectAtIndex:5];//here is an error occurred !!
There are a couple of issues with your code:
icounter so I presume that it has been initialized outside the snippet, or?countarray has to be large enough to hold all the rows or you will overwrite some unallocated memory. Maybe you know this beforehand or else you should find out e.g. by executing some SQL statement aka"SELECT COUNT(*) FROM Bcars".ior[ar count]whether you actually received these entries?sqlite3_finalize()