I have a sqlite3 database with a table "student":
If I try the query from terminal I get:
"Henrique Gonzales 1981021"
I am trying to do this with the sqlite3 framework available in xcode for iOS.This code runs on a background thread:
None of the assertions fails, but this ends up in an infinite loop (in the first loop).If I try printing the result the first time they're correct, but it seems like it doesn't exit from the loop.Other values are invalid values like an id of zero and all null strings.
Code:
create table student (name varchar(20) not null,
surname varchar(20) not null, id integer primary key);
If I try the query from terminal I get:
"Henrique Gonzales 1981021"
I am trying to do this with the sqlite3 framework available in xcode for iOS.This code runs on a background thread:
Code:
- (void) query : (id) sender
{
NSMutableArray* rows=[NSMutableArray new];
NSBundle* bundle=[NSBundle mainBundle];
NSString* path=[bundle pathForResource: @"test" ofType: @"db"];
sqlite3* database;
sqlite3_stmt* statement;
NSAssert(path, @"Invalid path for resource");
NSAssert( sqlite3_open([path UTF8String], &database)==SQLITE_OK, @"Failed to open the database");
NSAssert(sqlite3_prepare_v2(database, "select * from student", -1, &statement, NULL) == SQLITE_OK, @"Failed to prepare");
while(sqlite3_step(statement))
{
NSMutableArray* row= [NSMutableArray arrayWithCapacity: 3];
for (int i=0; i<sqlite3_column_count(statement); i++)
{
if(i==2)
{
const int studentId=sqlite3_column_int(statement, i);
[row addObject: @(studentId)];
}
else
{
const unsigned char * const column = sqlite3_column_text(statement, i);
[row addObject: [NSString stringWithFormat: @"%s",column]];
}
}
[rows addObject: row];
}
NSAssert(sqlite3_close(database)==SQLITE_OK, @"Database not closed correctly");
for(NSArray* row in rows)
{
NSLog(@"Name: %@ Surname: %@ Id: %@ ",row[0],row[1],row[2]);
}
}
None of the assertions fails, but this ends up in an infinite loop (in the first loop).If I try printing the result the first time they're correct, but it seems like it doesn't exit from the loop.Other values are invalid values like an id of zero and all null strings.