我正在使用在应用程序委托处打开的 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 中的项目树中,它位于具有读写权限的文件系统中的项目目录中。
谢谢