-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];

私はあなたがアイデアを得ることを願っています..更新を行うためにデータベースにアクセスする関数にquerystring(update sqlステートメント)を渡します。次回は、同じ更新ステートメントがあります。queryStringを作成して、関数に渡します。

于 2013-01-29T10:44:52.963 に答える