Update issue with sqliteManager

2019-09-04 23:08发布

I'm not able to do an update, but inserts and selects work fine.

Note:

1) I'm using the wrapper from: https://github.com/misato/SQLiteManager4iOS

2) The same code works for the INSERT statement, but not for the update

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

3) Here is the code:

The code associated with this update as follows:

my inline code

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

from the library:

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

2条回答
Ridiculous、
2楼-- · 2019-09-04 23:44

I found out the pitfall. I have wrapped the string using the method: initWithFormat.

I just assign a variable like this:

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

And now I just pass that variable in:

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

TODO/TO_ASK:

Why this same thing does NOT work with:

 NSString* sqlStr = [NSString stringWithFormat:@"UPDATE User SET Name = 'some name' WHERE sbuId = 19"];
查看更多
Bombasti
3楼-- · 2019-09-04 23:49

I would try with "WHERE Id = '19'" (19 with single quotes) because the field may have been defined as text since sqlite fields are weakly typed.

查看更多
登录 后发表回答