数据不在iphone的sqlite数据库中更新

时间:2012-11-28 04:34:19

标签: iphone sqlite

我已经在我的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");
    }

}

///从db读取的函数

-(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);

}

}

1 个答案:

答案 0 :(得分: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");
}