EGODatabase does not retain inserted record
I'm using EGODatabase to process my data. When I tried to insert data into the database which I created with Base and attached to the project, the data that was inserted programmatically will not show up when I restart my app.However the data inserted manually using Base will show up just fine.
Following is the code for inserting data into the database:
NSString *path = [[NSBundle mainBundle] pathForResource:@"Database" ofType:@"db"];
db = [EGODatabase databaseWithPath:path];
if ([db open]) {
NSLog(@"Database is opened successfully!");
} else {
NSLog(@"Database is failed to open!");
}
NSArray *params = [NSArray arrayWithObjects:[aFeed feedID], [aFeed title], nil];
[db executeUpdate:@"Insert INTO Feed (FeedID, Title) VALUES (?, ?)" parameters:params];
EGODatabaseResult *results = [db executeQuery:@"SELECT * FROM Feed"];
for ( EGODatabaseRow * row in results) {
NSLog(@"ID: %@ Title: %@", [row stringForColumn:@"FeedID"], [row stringForColumn:@"Title"]);
}开发者_JAVA百科
I appreciate if anyone can share me a working sample code for EGODatabase. I'm using EGODatabase because I might have multi-threading code that write to the database. Thank in advance.
You need to copy the database file from your bundle to the user's Documents directory before making changes to it. The bundle resources are read-only, that's why you can't update your data.
Put this in your - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
method:
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject] stringByAppendingPathComponent:@"Database.sqlite"];
NSFileManager *fileManager = [[NSFileManager alloc] init];
if (![fileManager fileExistsAtPath:filePath]) {
NSError *error = nil;
[fileManager copyItemAtPath:[[NSBundle mainBundle] pathForResource:@"Database" ofType:@"sqlite"] toPath:filePath error:&error];
}
[fileManager release];
精彩评论