-4

我在我的 iPhone 应用程序中实现了 sqlite 数据库。我可以从数据库中读取,但数据库甚至没有更新。代码如下。提前致谢

////// 将数据库复制到文件管理器的函数

-(void)copyToFileManager
{

    NSString *databaseName = @"kwest.sqlite";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    NSFileManager *fileManager = [NSFileManager defaultManager];
    BOOL success=[fileManager fileExistsAtPath:databasePath];

    if (!success) {

        NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

        // Copy the database from the package to the users filesystem
        [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];  
    }
    else{
        NSLog(@"file Exists");
    }

}

/// 从数据库读取的函数

-(NSMutableArray *)getQuestList{

    NSMutableArray *qusArray = [[NSMutableArray alloc]init];
    @try {
       NSString *databaseName = @"kwest.sqlite";
       // Get the path to the documents directory and append the databaseName
       NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
       NSString *documentsDir = [documentPaths objectAtIndex:0];
       NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

       if(!sqlite3_open([databasePath UTF8String],&data)==SQLITE_OK){
          NSLog(@"An error occured");

       }
       const char *sql ="SELECT *from Quest"; 
       sqlite3_stmt *sqlStatement;
       if(sqlite3_prepare(data , sql, -1, &sqlStatement, NULL) != SQLITE_OK)
       {
           NSLog(@"Problem with prepare statement");
       }
       while (sqlite3_step(sqlStatement)==SQLITE_ROW) {
          Quest *questlist = [[Quest alloc]init];
          questlist.q_id = sqlite3_column_int(sqlStatement, 0);
          questlist.q_name = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement,1)];
           questlist.solved = sqlite3_column_int(sqlStatement, 2);
           questlist.hint1 = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 3)];
           questlist.hint2 = [NSString stringWithUTF8String:(char *) sqlite3_column_text(sqlStatement, 4)];
           questlist.levelactive = sqlite3_column_int(sqlStatement, 5);
           [qusArray addObject:questlist];
       }


    }
    @catch (NSException *exception) {
       NSLog(@"An exception occured:%@",exception);
   }
   @finally {
     sqlite3_close(data);
     return qusArray;
   }
 }

/////更新数据库的函数

-(void)updateQuestSolved:(NSInteger)solved:(NSInteger)q_id{

    NSString *databaseName = @"kwest.sqlite";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    NSString *databasePath = [documentsDir stringByAppendingPathComponent:databaseName];

    @try {
        if(!sqlite3_open([databasePath UTF8String],&data)==SQLITE_OK){
            NSLog(@"An error occured");

    }
    NSString *querystr = [NSString stringWithFormat:@"Update Quest set solved = '%d' where q_id = '%d'",solved,q_id+1 ];
  //  const char *sql ="Update Quest set solved=? where q_id=?"; 
    sqlite3_stmt *sqlStatement;
    if(sqlite3_prepare_v2(data , [querystr UTF8String], -1, &sqlStatement, NULL) == SQLITE_OK)
    {
        NSLog(@"query executed");
    }
   /* sqlite3_bind_int(sqlStatement, 0, q_id+1);
    sqlite3_bind_int(sqlStatement, 2 , solved);  
    char* errmsg;
    sqlite3_exec(data, "COMMIT", NULL, NULL, &errmsg);

    if(SQLITE_DONE != sqlite3_step(sqlStatement))
        NSLog(@"Error while updating. %s", sqlite3_errmsg(data));
    else 
         sqlite3_reset(sqlStatement);
    *///      
}
@catch (NSException *exception) {
    NSLog(@"An exception occured:%@",exception);
}
@finally {
    sqlite3_close(data);

}

}
4

1 回答 1

1

这条线导致了这个问题。

NSString *querystr = [NSString stringWithFormat:@"Update Quest set solved = '%d' where q_id = '%d'",solved,q_id+1 ];

字段solvedq_id是整数类型。你正在用字符串检查它,这就是问题所在。

将查询字符串更改为:

NSString *querystr = [NSString stringWithFormat:@"Update Quest set solved = %d where q_id = %d",solved,q_id+1 ];

还要更改准备条件,例如:

if(sqlite3_prepare_v2(data , [querystr UTF8String], -1, &sqlStatement, NULL) == SQLITE_OK)
{
   sqlite3_step(sqlStatement);
   NSLog(@"query executed");
}
于 2012-11-28T04:52:08.930 回答