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 回答