Objective-C:带有多个where子句

时间:2016-01-14 20:57:53

标签: objective-c sqlite

我想从我的表中删除一行。首先,我使用createUserTable方法创建表。现在我想删除一行 - 例如userId = 4和groupId = 5.我在删除行方法中的if语句中得到false。 如何使用多个位置修复此删除?

+(BOOL)createUserTable:(sqlite3*)database{
    char* errormsg;

    NSString* sql = [NSString stringWithFormat:@"CREATE TABLE IF NOT EXISTS %@ (%@ TEXT, %@ TEXT)",USER_GROUPS_TABLE,USER_ID,GROUP_ID];
    int res = sqlite3_exec(database, [sql UTF8String], NULL, NULL, &errormsg);
    if(res != SQLITE_OK){
        NSLog(@"ERROR: failed creating USERS-GROUPS table");
        return NO;
    }
    return YES;
}

+(void)addRow:(sqlite3*)database userId:(NSString*)userId groupId:(NSString*)groupId{
    NSLog(@"add Row: userId = %@ groupId = %@ in Sql", userId, groupId);
    sqlite3_stmt *statment;
    NSString* query = [NSString stringWithFormat:@"INSERT OR REPLACE INTO %@ (%@,%@) values (?,?);",USER_GROUPS_TABLE,USER_ID,GROUP_ID];

    if (sqlite3_prepare_v2(database,[query UTF8String],-1,&statment,nil) == SQLITE_OK){
        sqlite3_bind_text(statment, 1, [userId UTF8String],-1,NULL);
        sqlite3_bind_text(statment, 2, [groupId UTF8String],-1,NULL);
        if(sqlite3_step(statment) == SQLITE_DONE){
            NSLog(@"addRow done");
            sqlite3_finalize(statment);
            return;
        }
    }
    sqlite3_finalize(statment);
    NSLog(@"ERROR: addRowToTable failed %s",sqlite3_errmsg(database));
}

+(void)removeRow:(sqlite3*)database userId:(NSString*)userId groupId:(NSString*)groupId{
    NSLog(@"removeRow: userId = %@ groupId = %@ from Sql", userId, groupId);
    sqlite3_stmt *statment;
    NSString* query = [NSString stringWithFormat:@"DELETE FROM %@ where US_ID = ? AND GR_ID = ?);",USER_GROUPS_TABLE];

    if (sqlite3_prepare_v2(database,[query UTF8String],-1,&statment,nil) == SQLITE_OK){
        sqlite3_bind_text(statment, 1, [userId UTF8String],-1,NULL);
        sqlite3_bind_text(statment, 2, [groupId UTF8String],-1,NULL);
        if(sqlite3_step(statment) == SQLITE_DONE){
            NSLog(@"removeRow done");
            sqlite3_finalize(statment);
            return;
        }
    }
    sqlite3_finalize(statment);
    NSLog(@"ERROR: removeRowFromTable failed %s",sqlite3_errmsg(database));
}

1 个答案:

答案 0 :(得分:0)

我的错误。我在“;”之前删除了“)”

NSString* query = [NSString stringWithFormat:@"DELETE FROM %@ where US_ID = ? AND GR_ID = ?);",USER_GROUPS_TABLE];