【问题标题】:Retrieve matching user data检索匹配的用户数据
【发布时间】:2013-09-07 04:57:55
【问题描述】:

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

代码:

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" 数据。如何在选择查询中将ObjForKey=@"id"viewController 传递到ViewController1.mCategoryNames & categoryIds 包含特定用户产品名称和 ID 的数组。如果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];

        }];

【问题讨论】:

  • 这适用于多个用户。每个用户在我的服务器中注册他们的名字。注册后,我的服务器将为每个用户生成一个用户 ID。然后每个用户写一些东西。这存储在每个用户中。如果用户安装应用程序并输入用户名、密码,它将转到 viewcontroller1.m 。在具有按钮的 viewcontroller1.m 中,如果单击按钮,我需要从服务器检索特定的用户数据。
  • @Meda:你明白了吗?
  • 您是在问如何将 id 从一个视图传递到另一个视图?
  • 你是对的。但是我怎样才能将这个 ID 传递给 sql 查询呢? const char *sql = "SELECT id,cat_name FROM categories where company_id = ObjForKey=@"id";

标签: iphone arrays json sqlite login


【解决方案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];

【讨论】:

  • 你是对的。但是我怎样才能将这个 ID 传递给 sql 查询呢? const char *sql = "SELECT id,cat_name FROM categories where company_id = ObjForKey=@"id";
  • @user2674668 你看到我回答的第二部分我展示了如何取回 ID,你需要帮助的部分
  • 没错。但是如何传递到这一行: SELECT id,cat_name FROM categories where company_id = ObjForKey=@"id"
  • @user2674668 我添加了更多细节,如果它有效,请告诉我,我们会解决这个问题
  • 我没有使用 NSString。我应该在下面使用: const char *sql = "SELECT id,cat_name FROM categories where company_id = %d", id1;
猜你喜欢
  • 2016-11-09
  • 2019-08-21
  • 2020-09-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-04-25
  • 2021-09-17
相关资源
最近更新 更多