Reading Data from sqlite
-(NSMutableArray *)GetAllOrdersDetails:(NSString *)UserID {
[self createEditableCopyOfDatabaseIfNeeded];
NSMutableArray *Array = [[[NSMutableArray alloc] init] autorelease];
if (sqlite3_open([[self GetDBPath] UTF8String], &database) == SQLITE_OK) {
NSString *sql =[NSString stringWithFormat:@"SELECT DISTINCT ID, Date,Price FROM Orders where UserID = '%@' ORDER BY _id DESC LIMIT 5",UserID];
//NSLog(@"%@",sql);
static sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
//sqlite3_bind_text(statement, 1, [orderID UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(statement) == SQLITE_ROW) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)] forKey:@"ID"];
[dict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)] forKey:@"Date"];
[dict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)] forKey:@"Price"];
[Array addObject:dict];
[dict release];
dict=nil;
}
sqlite3_finalize(statement);
}
else {
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
}
} else {
sqlite3_close(database);
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
}
return Array;
}
[self createEditableCopyOfDatabaseIfNeeded];
NSMutableArray *Array = [[[NSMutableArray alloc] init] autorelease];
if (sqlite3_open([[self GetDBPath] UTF8String], &database) == SQLITE_OK) {
NSString *sql =[NSString stringWithFormat:@"SELECT DISTINCT ID, Date,Price FROM Orders where UserID = '%@' ORDER BY _id DESC LIMIT 5",UserID];
//NSLog(@"%@",sql);
static sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL) == SQLITE_OK) {
//sqlite3_bind_text(statement, 1, [orderID UTF8String], -1, SQLITE_TRANSIENT);
while (sqlite3_step(statement) == SQLITE_ROW) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 0)] forKey:@"ID"];
[dict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 1)] forKey:@"Date"];
[dict setObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 2)] forKey:@"Price"];
[Array addObject:dict];
[dict release];
dict=nil;
}
sqlite3_finalize(statement);
}
else {
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
}
} else {
sqlite3_close(database);
NSAssert1(0, @"Failed to open database with message '%s'.", sqlite3_errmsg(database));
}
return Array;
}
Comments
Post a Comment