当我尝试它时,我无法更新我的数据库名称保持与以前相同并且没有更改。 我该怎么做才能使它工作,因为我认为查询语句是正确的
这是我的代码:
//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);
}
答案 0 :(得分:2)
使用此
更改您的查询字符串NSString*sql= [NSString stringWithFormat:@"UPDATE summary SET customer = '%@' WHERE key= '%@'", editCustomer,detailController.customerName];
这可能会解决您的问题
答案 1 :(得分: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;
}