【问题标题】:How to insert only one username and password in SQLite database?如何在 SQLite 数据库中只插入一个用户名和密码?
【发布时间】:2011-12-31 22:57:44
【问题描述】:

我有一个应用程序,其中有两个文本字段和一个按钮。在第一个文本字段中,我输入用户的用户名,在第二个文本字段中,我输入用户的密码。当用户输入用户名和密码并单击创建用户按钮时,将调用一个后端 api,它将注册用户的用户名和密码,当时我想在本地 SQLite 数据库中输入用户的用户名和密码加密格式。

当用户注册时,该特定用户的用户名应该显示在第一个文本字段中,并且它不应该是可编辑的,并且密码字段应该是可编辑的。整个应用程序只允许注册一个用户。

【问题讨论】:

  • 在添加用户之前检查你的数据库是否是他们的任何用户,如果有然后弹出其他明智的注册。
  • @Ron 其实我很困惑,你能详细解释一下吗
  • 当您的登录视图在视图中打开时,您确实出现了您的用户表的一个选择查询,它返回用户数组并检查它的计数是否为 0,如果为零则新用户注册,否则弹出用户 一位用户已经注册。
  • @Ron 我已经按照您所说的进行了尝试,但问题是用户名和密码不会在我的整个应用程序中保留,即当我单击导航栏后退按钮并返回到存在用户名和密码 texfields 它们在用户名中是空值
  • @Ron 我希望在整个应用程序中只保留一个用户名和密码。

标签: iphone objective-c


【解决方案1】:

如果我正确理解了您的要求,您可以遵循以下指南。

在向表中插入数据之前,您可以检查表中是否添加了任何记录。
如果计数为0,则可以继续插入记录,否则会提示错误。

我认为这将是最简单的方法。

【讨论】:

  • 我很困惑,你能详细解释一下吗
  • 添加一个简单的 if 语句来检查数据库是否包含记录。如果条件为假,则只进行插入逻辑,否则提示错误或任何您想要的不插入数据。
【解决方案2】:

第一步是检查资源文件夹中是否存在数据库并创建数据库:-

1)

-(void)checkAndCreateDatabase{
    databaseName = @"databasename.sql";

    NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDir = [documentPaths objectAtIndex:0];
    databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
    BOOL success;

    // Create a FileManager object, we will use this to check the status of the database and to copy it over if required
    NSFileManager *fileManager = [NSFileManager defaultManager];

    // Check if the database has already been created in the users filesystem
    success = [fileManager fileExistsAtPath:databasePath];

    // If the database already exists then return without doing anything
    if(success) {
        return;
    }
    // If not then proceed to copy the database from the application to the users filesystem

    // Get the path to the database in the application package
    NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];

    // Copy the database from the package to the users filesystem
    [fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}

2) 调用函数获取数据:-

-(NSMutableArray *) readFromDatabase:(NSString *)query{
    // Setup the database object
    sqlite3 *database;

    // Init the animals Array
    returnArray = [[NSMutableArray alloc] init];
    NSString *readCommand= query;

    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
        // Setup the SQL Statement and compile it for faster access
        const char *sqlStatement = [readCommand UTF8String];
        sqlite3_stmt *compiledStatement;

        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            // Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
                // Read the data from the result row
                [returnArray addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)]];
                [returnArray addObject:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]];
            }

        }

        // Release the compiled statement from memory
        sqlite3_finalize(compiledStatement);

    }
    sqlite3_close(database);
    return returnArray;
}

3) 检查 retrun 数组是否为空或 nil 然后调用步骤中定义的插入函数

4) 插入数据库

-(void)insertIntoDatabase:(NSString *)username:(NSString *)password{
    // Setup the database object

    sqlite3 *database;
    // Open the database from the users filessytem
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {

        // Setup the SQL Statement and compile it for faster access
        NSString *insertCommand=@"Insert into tableName values(username,password)";     
        const char *sqlStatement = [insertCommand UTF8String];

        sqlite3_stmt *compiledStatement;

        if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) {
            // Loop through the results and add them to the feeds array
            while(sqlite3_step(compiledStatement) == SQLITE_ROW) {}
            sqlite3_finalize(compiledStatement);
        }

        // Release the compiled statement from memory

    }
    sqlite3_close(database);

}

【讨论】:

  • 确保您已经在资源文件夹中创建了sqlite数据库,表内有两个字段用户名和密码。
【解决方案3】:

不要使用 sqlite dbs 来存储用户凭据。通过逆向工程,获取数据相当简单。这是因为您在用于加密/解密密码的程序中可能有一些价值。为安全起见,请使用钥匙串。 github上有几个项目,这使得使用钥匙串变得非常容易。没有比钥匙串更安全的方式来保存凭据了,所以请使用它来存储您的凭据!!!

【讨论】:

    【解决方案4】:

    正如cyberK所说,它不安全。钥匙串的使用可以参考post

    【讨论】:

      【解决方案5】:

      您可以使用 NSUserDefaults 将其保存在本地。以下代码用于保存数据。

       perf = [NSUserDefaults standardUserDefaults];
       [perf setObject:@"1" forKey:@"remember"];
       [perf setObject:emailAddresTextField.text forKey:@"email"];
       [perf setObject:passwordTextField.text forKey:@"password"];
       [perf synchronize];
      

      对于检索数据,请使用以下代码

      perf=[NSUserDefaults standardUserDefaults];
      NSString *remString = [perf stringForKey:@"remember"];  
      
      if ([remString isEqualToString:@"1"]) {
          emailAddresTextField.text=[perf stringForKey:@"email"];
          passwordTextField.text = [perf stringForKey:@"password"];
          [rememberBtn setTag:1];
          [rememberBtn setImage:[UIImage imageNamed:@"on_radioButton.png"] forState:0];
      }
      

      【讨论】:

      • 好吧,userdefaults 将用户凭据存储在其中并不是很安全...您真的不想将这种方法用于机密信息...
      猜你喜欢
      • 2017-07-18
      • 2017-08-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-08
      • 1970-01-01
      • 2018-02-13
      • 2012-08-29
      相关资源
      最近更新 更多