3

iPhone の iOS 4 で SQLite を使用していますが、更新ステートメントによる変更が保存されません。最初は、アプリを終了するとデータベースが何らかの形で削除されるのではないかと考えられましたが、同じセッション中には保持されません。データベースを初期化するコードは次のとおりです (FMDB を使用):

-(SQLiteDal*) init {
    pool = [[NSAutoreleasePool alloc] init];

    self = [super init];
    if(self !=  nil){
        // Setup some globals
        NSString *databaseName = [self getDbPath];
        NSLog([NSString stringWithFormat:@"db path: %@", databaseName]);
        db = (FMDatabase *)[FMDatabase databaseWithPath:databaseName];
        if (![db open]) {
            NSLog(@"Could not open db.");
            [pool release];
            return 0;
        }
    }
    //[self checkAndCreateDatabase];
    return self;
}

#pragma mark DB Maintenance
-(NSString *)getDbPath {
    NSString *databaseName = @"myapp.db";

    // Get the path to the documents directory and append the databaseName
    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databaseName = [documentsDir stringByAppendingPathComponent:databaseName];
    return databaseName;    
}

これらのメソッド両方とも、データベースを作成するために呼び出され、呼び出したテーブルに挿入されます。

            [db executeQuery:@"INSERT INTO MyTable (Name, Description, Area, Price, ID) VALUES (?,?,?,?,?)", 
             f.Name,
             f.description,
             f.area,
             f.price,
             f.id];

問題は、以下のステートメントを使用して読み取るようになるとMyTable、何も返されないことです。

    FMResultSet *rs = [db executeQuery:@"SELECT * FROM MyTable WHERE ID = ?", id];
    while ([rs next]) {
        //.. this is never called

私が見る限り、何も見逃していません。DB は書き込み可能な場所にあるようです。

4

1 に答える 1

6

executeUpdate挿入するときは、 notを呼び出す必要がありますexecuteQuery。また、次のようにbeginTransaction、次にを呼び出す必要があります。commit

[_dbPointer beginTransaction];
BOOL isInserted = [_dbPointer executeUpdate:[NSString stringWithFormat:@"INSERT INTO  MyTable (Name, Description, Area, Price, ID) VALUES(?,?,?,?,?);", f.Name,
         f.description,
         f.area,
         f.price,
         f.id]];
[_dbPointer commit];
于 2011-08-10T09:56:05.097 に答える