-3

所以是的,我是新手,我需要一些帮助。我有 2 个文本字段,上面有文本,我希望在我的数据库中更新这 2 个字符串(用户名、密码)。我不知道如何问它,因为我不知道这东西是如何工作的。对 Xcode 来说是全新的。请理解。谢谢!顺便说一句,这是我的代码 atm。

@interface ViewController4 ()

@end

@implementation ViewController4

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    self.lbName.text=self.strName;
    self.lbUser.text = self.strUsername;

    [self openDB];
    NSString *sql = [NSString stringWithFormat:@"SELECT username,password FROM SUMMARY8 WHERE name = '%s' and theuser = '%s'",[self.lbName.text UTF8String],[self.lbUser.text UTF8String]];

    sqlite3_stmt *statement;
    if (sqlite3_prepare(account, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
    {
        while (sqlite3_step(statement)==SQLITE_ROW){
        char *field1=(char *) sqlite3_column_text(statement, 0 );
        self.tbUsername.text=[[NSString alloc]initWithUTF8String:field1];
            char *field2=(char *) sqlite3_column_text(statement, 1 );
            self.tbPassword.text=[[NSString alloc]initWithUTF8String:field2];


    }
    }

    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void) openDB
{
    if (sqlite3_open([[self filePath] UTF8String], &account) !=SQLITE_OK){
        sqlite3_close(account);
        NSAssert(0,@"Database failed to open");

    }
    else
    {
        NSLog(@"database opened");
    }
}
-(NSString *) filePath {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0]stringByAppendingPathComponent:@"account.sql"];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)btnEdit:(id)sender {
    self.btnEdit.enabled=NO;
    self.tbUsername.enabled=YES;
    self.tbPassword.enabled=YES;
    self.btnUpdate.hidden=FALSE;

}
- (IBAction)btnReturn:(id)sender {
   FlipsideViewController *FVC = [self.storyboard instantiateViewControllerWithIdentifier:@"FlipsideViewController"];
     FVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    FVC.strUsername=self.strUsername;
    [self presentViewController:FVC animated:YES completion:nil];
}

- (IBAction)btnUpdate:(id)sender {

    [self openDB];
    NSString *sql = [NSString stringWithFormat:@"UPDATE SUMMARY8 SET username='1',password='2' WHERE name = '%s' and theuser = '%s'",[self.lbName.text UTF8String],[self.lbUser.text UTF8String]];
    sqlite3_stmt *statement;
    if (sqlite3_prepare(account, [sql UTF8String], -1, &statement, nil)==SQLITE_OK)
    {

    }


}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [super touchesBegan:touches withEvent:event];
    [[self tbUsername]resignFirstResponder];
    [[self tbPassword]resignFirstResponder];

}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return [textField resignFirstResponder];
}
@end
4

1 回答 1

0

这就是我要做的(相应地改变它):

-(void)updateStatus:(NSString *)queryString {
    NSString    *docsDir;
    NSArray     *dirPaths;
    dirPaths    = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    docsDir     = [dirPaths objectAtIndex:0];

    strDatabasePath         = [NSString stringWithString:[docsDir stringByAppendingPathComponent:@"database.db"]];
    NSFileManager *filemgr  = [NSFileManager defaultManager];

    if ([filemgr fileExistsAtPath: strDatabasePath] == YES)
    {
        const char *dbpath = [strDatabasePath UTF8String];
        if (sqlite3_open(dbpath, &sqlDatabase) == SQLITE_OK)
        {
            const char* beginString = "BEGIN;";
            sqlite3_stmt *compiledstatement;
            sqlite3_prepare_v2(sqlDatabase, beginString, -1, &compiledstatement, NULL);
            if (sqlite3_step(compiledstatement) == SQLITE_DONE) {}
            else DLog(@"Failed!");
            sqlite3_finalize(compiledstatement);

            DLog(@"QUERY : %@",queryString);

            const char *selectStatement = [queryString UTF8String];

            sqlite3_prepare_v2(sqlDatabase, selectStatement, -1, &compiledstatement, NULL);
            //sqlite3_bind_text(compiledstatement,1,[statusString UTF8String],-1,SQLITE_TRANSIENT);

            if (sqlite3_step(compiledstatement) == SQLITE_DONE) {}
            else DLog(@"Failed!");
            sqlite3_finalize(compiledstatement);


            const char* endString="END;";
            sqlite3_prepare_v2(sqlDatabase, endString, -1, &compiledstatement, NULL);
            if (sqlite3_step(compiledstatement) == SQLITE_DONE) {}
            else DLog(@"Failed!");
            sqlite3_finalize(compiledstatement);

            sqlite3_close(sqlDatabase);
        }
        else DLog(@"Failed to open table");
    }
}

NSString *queryString;
queryString = [NSString stringWithFormat: @"UPDATE SUMMARY8 SET username='1',password='2' WHERE name = '%s' and theuser = '%s'",[self.lbName.text UTF8String],[self.lbUser.text UTF8String]];
[self updateStatus:queryString];

我希望你明白.. 将查询字符串(更新 sql 语句)传递给将接触数据库进行更新的函数。下次,您有相同的更新语句,只需创建 queryString 并将其传递给函数。

于 2013-01-29T10:44:52.963 回答