2013-02-12 56 views
0

我有数据库,但是当我试图从数据库中删除数据没有任何反应,我该怎么做才能确保它的工作?因为当我按下删除DTA仍然在数据库无法从数据库中删除数据ios

这是代码:

/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"); 
     } 


    } 


    - (IBAction)del:(id)sender { 

     NSString*sql = [NSString stringWithFormat:@"DELETE key, theDate, customer, code1, code2 FROM summary WHERE key=\"%@\"",customerName]; 
     const char* query_stmt = [sql UTF8String]; 
     sqlite3_stmt*statement; 

     sqlite3_prepare_v2(db, query_stmt, -1, & statement, NULL); 
     if (sqlite3_step(statement) == SQLITE_DONE) 
     { 
      NSAssert(0, @"database object delete failed"); 

     } else { 
      NSLog(@"No error"); 

     } 
     sqlite3_finalize(statement); 
     sqlite3_close(db) 
+0

什么是你想实现什么?你是想删除整行还是仅仅清除一行中的某些值? – zaph 2013-02-12 11:07:18

+0

是你的问题解决? – Rajneesh071 2013-02-12 12:06:58

+0

是问题解决:) – Thymen 2013-02-12 12:40:49

回答

3

不能删除使用DELETE查询特定的列值。它用于删除整行。

enter image description here

的问题是用下面的查询:

NSString*sql = [NSString stringWithFormat:@"DELETE key, theDate, customer, code1, code2 FROM summary WHERE key=\"%@\"",customerName]; 

将其更改为:

NSString*sql = [NSString stringWithFormat:@"DELETE FROM summary WHERE key=\"%@\"",customerName]; 

如果您需要删除行的特定列值使用UPDATE查询。

请询问详细

+0

感谢您的帮助 – Thymen 2013-02-12 12:39:28

+0

@ user1980004:高兴:)感谢您的帮助 – 2013-02-12 12:46:30

0

你写像检查文件路径的所有功能检查sqlite documentation,opendb应该发生在同一个函数(也许你德尔函数内)。

这是我会怎么做:

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

    strDatabasePath   = [NSString stringWithString:[docsDir stringByAppendingPathComponent:@"bp.sql"]]; 
    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:@"DELETE key, theDate, customer, code1, code2 FROM summary WHERE key=\"%@\"",customerName]; 
[self updateStatus:queryString]; 

希望这有助于...

0
-(BOOL)DeleteWishList:(int)rowno 
{ 

NSString *queryString=[NSString stringWithFormat:@"delete from wishtable where _id=%d",rowno]; 

[self openDB]; 
char *err; 
if (sqlite3_exec(dBObject, [queryString UTF8String], NULL,NULL, &err)!= SQLITE_OK) 
{ 
    sqlite3_close(dBObject);  
    return NO; 
} 
else 
{ 
    return YES; 
} 

}