Skip to content

Instantly share code, notes, and snippets.

Created August 7, 2011 08:19
Show Gist options
  • Save anonymous/1130189 to your computer and use it in GitHub Desktop.
Save anonymous/1130189 to your computer and use it in GitHub Desktop.
Fetch All Employees
-(NSMutableArray *) getEmployees {
sqlite3 *database = EmployeeDatabase();
NSMutableArray *employees = [[[NSMutableArray alloc] init] autorelease];
NSString *nsquery = [[NSString alloc] initWithFormat:@"SELECT * FROM EMPLOYEE ORDER BY EMPLOYEE_ID"];
const char *query = [nsquery UTF8String];
[nsquery release];
sqlite3_stmt *statement;
int prepareCode = (sqlite3_prepare_v2( database, query, -1, &statement, NULL));
if(prepareCode == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
Employee *newEmployee = [[Employee alloc] init];
newEmployee.name = sqlite3_column_int(statement, 0);
newEmployee.age = sqlite3_column_int(statement, 2);
[employees addObject:newEmployee];
[newEmployee release];
}
sqlite3_finalize(statement);
}
return employees;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment