如何将rowId添加到此updateRow方法

时间:2014-03-21 13:44:53

标签: ios objective-c

我正在关注Lynda.com的iOS教程之一,它提供了示例代码。在BWDB.h文件(Bill Weinman数据库)中,有一个像这样声明的updateRow方法

- (void) updateRow:(NSDictionary *) record forRowID: (NSNumber *) rowID;

在导入BWDB.h的文件中,教师使用像此一样的updateRow方法

  [db updateRow:@{ @"a" : @"cuatro", @"b" : @"cinco", @"c" : @"seis" }
                     :[NSNumber numberWithInt:4]];
        for (row in [db getQuery:@"SELECT * FROM bwtable"]) {
            [self dispRow:row];
        }

它引发了一个错误,说“BWDB没有可见的@interface声明选择器updateRow”

这是因为该方法是updateRow:forRowId:并且他没有在导入BWDB.h的文件中使用forRowId部分吗?

我在下面使用updateRow复制了整个testDatabase方法。有没有办法为它添加forRowId以使其有效?

-(void) testDatabase {
    NSString *dbtn = @"bwtable";
    NSString *dbfn = @"bwtest.db";
    BWDB *db;

    NSDictionary * row = nil;
    if ((db = [[BWDB alloc] initWithDBFilename:dbfn andTableName: dbtn])) {
        message(@"BWDB version %@", [db getVersion]);
        message(@"SQLite version %@", [db valueFromQuery:@"select sqlite_version()"]);
        message(@"create table ...");
        [db doQuery:@"drop table if exists bwtable"];
        [db doQuery:@"CREATE TABLE bwtable (id INTEGER PRIMARY KEY, a, b, c, stamp TEXT DEFAULT CURRENT_TIMESTAMP)"];
        message(@"insert row with doQuery ...");
        [db doQuery:@"insert into bwtable (a, b, c) values (?, ?, randomblob(8))", nil, @"2" ];
        message(@"lastInsertId: %@", [db lastInsertId]);

        // NSNumber doubles
        message(@"CRUD insert, rowID: %@", [db insertRow: @{ @"a" : @2.0, @"b" : @3.0, @"c" : @4.0 }]);
        // NSNumber integers
        message(@"CRUD insert, rowID: %@", [db insertRow: @{ @"a" : @3, @"b" : @4, @"c" : @5 }]);
        // Mixed
        message(@"CRUD insert, rowID: %@", [db insertRow: @{ @"a" : @"four", @"b" : @5, @"c" : @6.42 }]);
        message(@"rows with getQuery ...");
        for (row in [db getQuery:@"SELECT * FROM bwtable"]) {
            [self dispRow:row];
        }
        message(@"udpateRow 4 ...");
        [db updateRow:@{ @"a" : @"cuatro", @"b" : @"cinco", @"c" : @"seis" }
                     :[NSNumber numberWithInt:4]];
        for (row in [db getQuery:@"SELECT * FROM bwtable"]) {
            [self dispRow:row];
        }
        message(@"count rows: %@", [db countRows]);
        message(@"deleteRow 3 ...");
        [db deleteRow:[NSNumber numberWithInt:3]];
        message(@"count rows: %@", [db countRows]);
        for (row in [db getQuery:@"SELECT * FROM bwtable"]) {
            [self dispRow:row];
        }
        message(@"getRow 4 ...");
        [self dispRow:[db getRow:[NSNumber numberWithInt:4]]];

    } else {
        message(@"db failed to init");
    }
}

1 个答案:

答案 0 :(得分:0)

是,

[db updateRow:@{ @"a" : @"cuatro", @"b" : @"cinco", @"c" : @"seis" } forRowID:[NSNumber numberWithInt:4]];

应该做的伎俩