=============================================
- (void) copyDatabaseIfNeeded {
//Using NSFileManager we can perform many file system operations.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSString *dbPath = [self getDBPath];
BOOL success = [fileManager fileExistsAtPath:dbPath];
if(!success) {
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"example.sql"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:dbPath error:&error];
if (!success)
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
else {
NSLog(@"open");
}
}
- (NSString *) getDBPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
NSString *documentsDir = [paths objectAtIndex:0];
return [documentsDir stringByAppendingPathComponent:@"example.sql"];
}
=====================================================
view control
========================================================
static sqlite3 *database = nil;
#import
appDelegate = (CompassDictionaryAppDelegate *)[[UIApplication sharedApplication] delegate];
[appDelegate copyDatabaseIfNeeded];
- (IBAction)MyFavoritesAction:(id)sender {
appDelegate.favorite = [NSMutableArray new];
[self SelectDatabase:[appDelegate getDBPath]];
MyFavorites *myfav = [[MyFavorites alloc] initWithNibName:@"MyFavorites" bundle:nil];
[self presentModalViewController:myfav animated:NO];
[myfav release];
}
------------------
-(void)SelectDatabase:(NSString*)DBPath{
appDelegate = (CompassDictionaryAppDelegate *)[[UIApplication sharedApplication] delegate];
if (sqlite3_open([DBPath UTF8String], &database) == SQLITE_OK) {
const char *sql = "select fav_title,fav_text from fav_list order by fav_title";
sqlite3_stmt *selectstmt;
if(sqlite3_prepare_v2(database, sql, -1, &selectstmt, NULL) == SQLITE_OK) {
while(sqlite3_step(selectstmt) == SQLITE_ROW) {
FavoriteData *data=[[FavoriteData alloc] init];
//sqlite3_bind_text(addStmt, 1, [data.name UTF8String], -1, SQLITE_TRANSIENT);
NSString *coffeeObj = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];
// NSLog(@"name man ->%@",coffeeObj);
data.description = coffeeObj;
NSString *coffeeObj1 = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 0)];
//NSLog(@"name man ->%@",coffeeObj1);
data.name = coffeeObj1;
//NSLog(@"favourite_id---->%i",(NSInteger)sqlite3_column_int(selectstmt, 0));
//NSLog(@"search_list_id-->%i",(NSInteger)sqlite3_column_int(selectstmt, 1));
//NSLog(@"%i",coffeeObj.userid);
[appDelegate.favorite addObject:data];
}
}
sqlite3_finalize(selectstmt);
}
NSLog(@"close database");
sqlite3_close(database); //Even though the open call failed, close the database connection to release all the memory.
//NSLog(@"favourite_id---->%i",(NSInteger)sqlite3_column_int(selectstmt2, 0));
//NSLog(@"search_list_id-->%i",(NSInteger)sqlite3_column_int(selectstmt2, 1));
}

No comments:
Post a Comment