0

我无法进行更新,但插入和选择工作正常。

笔记:

1)我使用的包装器来自:https ://github.com/misato/SQLiteManager4iOS

2) 相同的代码适用于 INSERT 语句,但不适用于更新

//NSString* sqlStr = (@"INSERT INTO sbu (sbuName) VALUES ( 'rrrr' );)"); //WORKS

3)这是代码:

与本次更新相关的代码如下:

我的内联代码

    NSString* sqlStr = (@"UPDATE User SET Name = 'wweerr' WHERE Id = 19"); //using a direct sql to verify if it works - does not
    SQLiteManager* dbManager =[[SQLiteManager alloc]initWithDatabaseNamed:@"data.db"];
    NSError* error = [dbManager doQuery:sqlStr];

从图书馆:

- (NSError *)doQuery:(NSString *)sql {

    NSError *openError = nil;
    NSError *errorQuery = nil;

    //Check if database is open and ready.
    if (db == nil) {
        openError = [self openDatabase];
    }

    if (openError == nil) {     
        sqlite3_stmt *statement;    
        const char *query = [sql UTF8String];
        sqlite3_prepare_v2(db, query, -1, &statement, NULL);

        if (sqlite3_step(statement) == SQLITE_ERROR) {
            const char *errorMsg = sqlite3_errmsg(db);
            errorQuery = [self createDBErrorWithDescription:[NSString stringWithCString:errorMsg encoding:NSUTF8StringEncoding]
                                                    andCode:kDBErrorQuery];
        }
        //NSLog(@"sql error: %@", error)
        NSInteger result =  sqlite3_finalize(statement);
        errorQuery = [self closeDatabase];
    }
    else {
        errorQuery = openError;
    }
4

2 回答 2

0

我会尝试使用“WHERE Id = '19'”(19 带单引号),因为该字段可能已被定义为文本,因为 sqlite 字段是弱类型的。

于 2012-07-18T12:46:49.107 回答
0

我发现了陷阱。我已经使用以下方法包装了字符串:initWithFormat

我只是分配一个这样的变量:

NSString *sqlStr = [[NSString alloc] initWithFormat:@"UPDATE User SET Name = 'some name' WHERE sbuId = 19"];

现在我只是将该变量传递给:

NSError* error = [[Utilities dbManager] doQuery:sqlStr];

TODO/TO_ASK:

为什么同样的事情不适用于:

 NSString* sqlStr = [NSString stringWithFormat:@"UPDATE User SET Name = 'some name' WHERE sbuId = 19"];
于 2012-07-18T17:46:32.140 回答