我正在使用我在app委托打开的sqlite3数据库,然后在按下按钮后在另一个视图中执行不起作用的更新语句。 应用委托代码:
- (BOOL) openDB{
//NSMutableArray *logoArray = [[NSMutableArray alloc] init];
@try {
NSFileManager *fileMgr = [NSFileManager defaultManager];
NSString *dbPath = [[[NSBundle mainBundle] resourcePath ]stringByAppendingPathComponent:@"SportsLogo.sqlite"];
BOOL success = [fileMgr fileExistsAtPath:dbPath];
if(!success)
{
NSLog(@"Cannot locate database file '%@'.", dbPath);
}
if(!(sqlite3_open([dbPath UTF8String], &db) == SQLITE_OK))
{
return NO;
}
}
@catch (NSException *exception) {
NSLog(@"An exception occured: %@", [exception reason]);
}
@finally {
return YES;
}
}
- (sqlite3 *) getDB{
return db;
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
//MySportsLogosDB *myDB = [[MySportsLogosDB alloc]init];
//NSMutableArray *myArray = [[NSMutableArray alloc]init];
if ([self openDB] != YES){
NSLog(@"Problem with opening the DB");
return NO;
}
return YES;
}
然后当我按下按钮时,我运行以下代码:
-(IBAction)enter:(id)sender
{
UIImage *answerImage = [UIImage imageNamed:[NSString stringWithFormat:@"%@%@",currentTitle, @".png"]];
if ([[currentTitle lowercaseString] isEqualToString:logoNameText.text]){
[imageGuessView setImage:answerImage];
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
// NSString *sql =[NSString stringWithFormat:@"select rowid from mytable where name = %@",str];
NSString *sql = [NSString stringWithFormat:@"UPDATE LogosTbl set logoStatus = ?1 WHERE logoName = '%@'",[currentTitle lowercaseString]];
sqlite3_stmt *sqlStatement;
if(sqlite3_prepare_v2([appDelegate getDB], [sql UTF8String], -1, &sqlStatement, NULL) != SQLITE_OK)
{
NSLog(@"Problem with prepare statement");
NSLog(@"%s",sqlite3_errmsg([appDelegate getDB]));
// printf( "could not prepare statemnt: %s\n", sqlite3_errmsg(database) );
}
if(SQLITE_DONE != sqlite3_step(sqlStatement)){
NSLog(@"Problem with UPDATE");
}
}
else{
NSLog(@"First No");
}
}
准备就绪,步骤也可以,但我没有看到数据已更新。 sqlite文件位于Xcode中的项目树中,它放在文件系统的项目目录中,并带有read&写权限。
谢谢
答案 0 :(得分:1)
如果数据库文件在项目的bundle中,那么你就不能在那里写任何东西,因为bundle只是readonly。将数据库文件复制到您的文档文件夹,然后您将拥有一个可写数据库。此示例使用sqlite3的FMDB包装器:
NSError *error = nil;
self.nfm = [[NSFileManager alloc] init];
/*
* if the database isn't in the Documents directory then copy from the bundle
*/
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *documentsDatabasePath = [documentsPath stringByAppendingPathComponent:@"orders.db"];
if(![self.nfm fileExistsAtPath:documentsDatabasePath]){
NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"orders" ofType:@"db"];
NSLog(@"Model init about to copy %@ to %@",bundlePath,documentsDatabasePath);
[self.nfm copyItemAtPath:bundlePath toPath:documentsDatabasePath error:&error];
}
self.database = [FMDatabase databaseWithPath:documentsDatabasePath];
if (![self.database open]) {
if(trace) NSLog(@"Model: start: Could not open database in path \n%@",documentsDatabasePath);
return nil;
}
if(trace) NSLog(@"Model: start: Opened database in path \n%@",documentsDatabasePath);