-4

iPhone アプリに sqlite データベースを実装しました。db から読み取ることはできますが、db は更新されていません。コードは次のとおりです。よろしくお願いします

////// データベースをファイルマネージャにコピーする関数

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

/////dbを更新する関数

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

フィールドsolvedとフィールドq_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 に答える