检索匹配的用户数据

时间:2013-09-07 04:57:55

标签: iphone arrays json sqlite login

我有用户名和密码的登录页面。当我输入正确的凭据时,它将转到另一个视图控制器。在此视图控制器中,我有按钮来单击匹配的用户信息。如果用户名匹配,我只需要检索他们的数据并显示。

代码:

viewController.m:

if ([responseData length]){

    NSLog(@"Response ==> %@", responseData);
    NSMutableDictionary *dict=[responseData JSONValue];                
    NSMutableDictionary *dict1=[responseData JSONValue];                
    NSInteger success = [(NSNumber *) [dict objectForKey:@"success"] integerValue];
    NSInteger id1 = [(NSNumber *) [dict1 objectForKey:@"id"] integerValue];               

   NSLog(@"%d",success);
    if(success == 1){
        NSLog(@"Login SUCCESS");
        [self alertStatus:@"Logged in Successfully." :@"Login Success!"];
        [self.navigationController pushViewController:overlay animated:YES];

    } else {
        NSString *error_msg = (NSString *) [dict objectForKey:@"error_message"];
        [self alertStatus:error_msg :@"Login Failed!"];
    }
}

以上代码objForKey=@"success"包含username & password. ObjForKey=@"id"包含每个用户ID。如果此id匹配,我需要显示该id数据。

viewController1.m:

我需要ObjForKey=@"id"。 company_id存储ObjForKey=@"id"数据。如何在select查询中将ObjForKey=@"id"viewController传递到ViewController1.mCategoryNames& categoryIds包含特定用户产品名称的数组& IDS。如果ObjForKey=@"id"匹配,我需要获得该用户categoryNames& categoryIds

if (sqlite3_open([path UTF8String], &database) == SQLITE_OK) {

    const char *sql = "SELECT id,cat_name FROM categories where company_id = ObjForKey=@"id";
    NSLog(@"sql is %s",sql);

    categoryNames = [NSMutableArray array];
    categoryIds = [NSMutableArray array];
    sqlite3_stmt *statement;
    // int catID = 0;
  if (sqlite3_prepare_v2(database, sql, -1, &statement, NULL) == SQLITE_OK) {

    // We "step" through the results - once for each row.
    while (sqlite3_step(statement) == SQLITE_ROW) {
        NSString *categoryName = [[NSString alloc] 
                                  initWithUTF8String:(const char *) sqlite3_column_text(statement, 1)];
        NSLog(@"catName is %@",categoryName);
        NSInteger categoryId = sqlite3_column_int(statement, 0);

        [categoryNames addObject:categoryName];
        [categoryIds addObject:@(categoryId)];
    } 
  }
}

此处UIActionSheet显示特定用户ID的categoryNames:

TSActionSheet *actionSheet = [[TSActionSheet alloc] initWithTitle:@"Design"];       


 for (int i = 0; i<[categoryNames count]; i++ ) {           

            NSLog(@"catearray is %@",categoryNames);

            [actionSheet addButtonWithTitle:[categoryNames objectAtIndex:i] block:^{

                [self actionSheetClickedButtonAtIndex:i];

        }];

1 个答案:

答案 0 :(得分:1)

您可以使用NSUserDefaults

if ([responseData length]){

    NSLog(@"Response ==> %@", responseData);
    NSMutableDictionary *dict=[responseData JSONValue];                                
    NSInteger success = [(NSNumber *) [dict objectForKey:@"success"] integerValue];

    NSLog(@"%d",success);
    if(success == 1){
        //save the ID
        NSInteger id1 = [(NSNumber *) [dict objectForKey:@"id"] integerValue];
        NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
        [userData setInteger:id1 forKey:@"id"];
        [userData synchronize];       
        NSLog(@"Login SUCCESS");
        [self alertStatus:@"Logged in Successfully." :@"Login Success!"];
        [self.navigationController pushViewController:overlay animated:YES];

    } else {
        NSString *error_msg = (NSString *) [dict objectForKey:@"error_message"];
        [self alertStatus:error_msg :@"Login Failed!"];
    }
}

然后要获得id值,您可以在ViewController1.m中执行此操作:

//Get the Id
NSUserDefaults *userData = [NSUserDefaults standardUserDefaults];
NSInteger id1 = [userData integerForKey:@"id"];

编辑:

  

但我如何将此Id传递给SQL查询?

不确定为什么在查询中objForKey尝试此操作:

NSString *sql  = [NSString stringWithFormat:
                    @"SELECT id,cat_name FROM categories where company_id = %d", id1];