0

データベースを更新しようとしても更新できません。名前は以前と同じままで、変更されませんでした。クエリステートメントは正しいと思うので、機能させるにはどうすればよいですか

これは私のコードです:

//file path to database
-(NSString*)filePath {
    NSArray*paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    return [[paths objectAtIndex:0]stringByAppendingPathComponent:@"bp.sql"];
}

//open database
-(void)openDB {

    if(sqlite3_open([[self filePath]UTF8String], &db) !=SQLITE_OK) {
        sqlite3_close(db);
        NSAssert(0, @"Databese failed to open");
    }

    else {
        NSLog(@"database opemed");
    }      
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

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

- (IBAction)edit:(id)sender {

    bool text = true;

    editCodeOne = code1Edit.text;
    editCodeTwo = code2Edit.text;
    editCustomer = customerEdit.text;
    editDate = [NSDate date];

    if ([code1Edit.text isEqualToString:@""] && [code2Edit.text isEqualToString:@""] && [customerEdit.text isEqualToString:@""]) {

        text =false;

    }

    if (text ==false) {
          UIAlertView*alert = [[UIAlertView alloc] initWithTitle:@"Edit failed" message:@"The data has not changed " delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
        [alert show];
    }

    if(text == true){

        NSString*sql= [NSString stringWithFormat:@"REPLACE summary SET customer = \"'%@'\" WHERE key= \"%@\"", editCustomer,detailController.customerName];
         const char* query_stmt = [sql UTF8String];
        sqlite3_stmt*statement;

        if( sqlite3_prepare_v2(db, query_stmt, -1, &statement, NULL )){
            if (sqlite3_step(statement)!= SQLITE_UPDATE) {

            sqlite3_close(db);
            NSAssert(0, @"Could not update table");
            }

        else {
            NSLog(@"table updated");
            code1Edit.text = @"";
            code2Edit.text = @"";
            customerEdit.text = @"";
        }

        }
        sqlite3_finalize(statement);
    }
4

2 に答える 2

2

これでクエリ文字列を変更します

NSString*sql= [NSString stringWithFormat:@"UPDATE summary SET customer = '%@' WHERE key= '%@'", editCustomer,detailController.customerName];

これはあなたの問題を解決するかもしれません

于 2013-02-21T12:24:06.337 に答える
0

これを試して、データベースがシミュレーターで作成されていることを確認してください

 -(int)update
{
        int i=0;

        @try
        {
            sqlite3_stmt *statement=nil;
            NSString  *sql=nil;

            sql= [NSString stringWithFormat:@"REPLACE summary SET customer = \"'%@'\" WHERE key= \"%@\"", editCustomer,detailController.customerName];;

            if(sqlite3_prepare_v2(database, [sql UTF8String], -1, &statement, NULL)!=SQLITE_OK)
            {
                NSAssert1(0, @"Error: failed to prepare statement with message '%s'.", sqlite3_errmsg(database));
            }



            int success=sqlite3_step(statement);

            if (success == SQLITE_ERROR)
            {
                NSAssert1(0, @"Error: failed to insert into the database with message '%s'.", sqlite3_errmsg(database));
            }

            i = sqlite3_last_insert_rowid(database);

            sqlite3_finalize(statement);
        }
        @catch (NSException *e)
        {

            NSLog(@"Insert Show data fail.");
        }

        return i;
    }
于 2013-02-21T10:10:30.680 に答える