2014-05-16 34 views
0

刪除我在那裏我試圖刪除存儲在瀏覽器sqilte數據的tableview應用。當我刪除模擬器中的數據時,關閉並重新運行模擬器後,數據不會再出現。然而,當我打開其中原始數據是使用SQLite瀏覽器保存在數據庫中,已刪除的數據仍顯示在瀏覽器的源碼。有人可以提醒我錯過了什麼嗎?泰伯維應用從SQLite數據庫

#import "DataAccess.h" 
#import "sqlite3.h" 
#import "Product.h" 
#import "Company.h" 


@implementation DataAccess 

NSString *dbPathString; 
sqlite3 *companyDB; 
sqlite3 *productDB; 


-(void) setCompanyListFromDB 
{ 
    NSLog(@"setCompanyListFromDB"); 
    dbPathString = @"/Users/user/Desktop/telecom.db"; 

    self.companyList = [self readCompanyDataFromDB]; 
    NSLog(@"read all company data"); 
} 


-(void) deleteCompany:(Company *)company andDeleteProduct:(NSIndexPath*)indexPath{ 

    //pass the company from the tableview, then pass the row from the tablewview 
    Product *product = [company.products objectAtIndex:indexPath.row]; 
    [self deleteProductFromDB:product.name]; 
    [company.products removeObjectAtIndex:indexPath.row]; 

} 


-(NSMutableArray*)readCompanyDataFromDB //returning companyList bc we want to use this for displaying in tableview 
{ 

    NSLog(@"readDataFromDB"); 

    NSMutableArray *companyList = [[NSMutableArray alloc]init]; 


    sqlite3_stmt *statement ; 


    if (sqlite3_open([dbPathString UTF8String], &companyDB)==SQLITE_OK) { //when we do this process for products, we can skip opening companyDB bc it is already open - just do it once 

     NSLog(@"sqlite3_open"); 


     // Reading Companies ....................................Start 
     NSLog(@"Reading Companies"); 
     NSString *querySQL = [NSString stringWithFormat:@"SELECT * FROM company"]; 
     NSLog(@"Company SQL: %@", querySQL); 
     const char *query_sql = [querySQL UTF8String]; //converting to type of string C understands 
     if (sqlite3_prepare(companyDB, query_sql, -1, &statement, NULL) == SQLITE_OK) 
     { 
      NSLog(@"sqlite3_prepare"); 
      while (sqlite3_step(statement)== SQLITE_ROW) //while stepping through database, if it returns a row it should keep going, cuts out if it returns something other than SQLITE_ROW 
      { 
       // 1|Apple|AAPL|593.1|apple.jpeg 

       NSString *companyID = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)]; 
       NSString *name = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)]; 
       NSString *stockSymbol = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)]; 
       NSString *stockPrice = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)]; 
       NSString *logo = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 4)]; 

       //Company *company = [Company initWithName:name logo:logo symbol:stockSymbol price:stockPrice]; 

       Company *company = [[Company alloc]init]; 
       company.companyID = companyID; 
       company.name = name; 
       company.logo = logo; 
       company.stockSymbol = stockSymbol; 
       company.stockPrice = stockPrice; 


       [company print]; 

       [companyList addObject:company]; 

      } 

     } 
     // Reading Companies ....................................Done 


     // Reading Products for each Company ....................................Start 

     sqlite3_close(companyDB); 


    } 


    for(Company *company in companyList){ 
     NSMutableArray *productList = [self readProductDataFromDBForCompanyID:company.companyID ]; 
     company.products = productList; 
    } 

    return companyList; 

} 


-(NSMutableArray*) readProductDataFromDBForCompanyID:(NSString*)companyId { 

    NSLog(@"readProductDataFromDB"); 
    NSMutableArray *productList = [[NSMutableArray alloc]init]; 


    sqlite3_stmt *statement ; 

    if (sqlite3_open([dbPathString UTF8String], &companyDB)==SQLITE_OK) { //when we do this process for products, we can skip opening companyDB bc it is already open - just do it once 

     NSLog(@"sqlite3_open"); 

     // Reading Products ....................................Start 
     NSLog(@"Reading Products"); 
     NSString *querySQL = [NSString stringWithFormat:@"select * from product where productid = %@", companyId]; 
     NSLog(@"Product SQL: %@", querySQL); 
     const char *query_sql = [querySQL UTF8String]; //converting to type of string C understands 
     if (sqlite3_prepare(companyDB, query_sql, -1, &statement, NULL) == SQLITE_OK) 
     { 
      NSLog(@"sqlite3_prepare"); 
      while (sqlite3_step(statement)== SQLITE_ROW) //while stepping through database, if it returns a row it should keep going, cuts out if it returns something other than SQLITE_ROW 
      { 

       NSString *name = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 0)]; 
       NSString *website = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 2)]; 
       NSString *productID = [[NSString alloc]initWithUTF8String:(const char *)sqlite3_column_text(statement, 3)]; 


       Product *product = [[Product alloc]init]; 
       product.productID = productID; 
       product.name = name; 
       product.website = website; 

       [product print]; 

       [productList addObject:product]; 
      } 

     } 
     // Reading Products ....................................Done 

     sqlite3_close(companyDB); 

    } 

// insert into product values('iPad','0','http://www.apple.com/ipad/',1); 


    return productList; 


} 

-(void) deleteProductFromDB:(NSString*)productname{ 

    if (sqlite3_open([dbPathString UTF8String], &companyDB)==SQLITE_OK) { //when we do this process for products, we can skip opening companyDB bc it is already open - just do it once 

     NSLog(@"sqlite3_open"); 

     NSString *querySQL = [NSString stringWithFormat:@"delete from product where productname = '%@'", productname]; 

     NSLog(@"Product Delete SQL: %@", querySQL); 
     const char *deleteQuery = [querySQL UTF8String]; //converting to type of string C understands 

     if (sqlite3_exec(companyDB, deleteQuery, NULL, NULL, nil)==SQLITE_OK) 
     { 
      NSLog(@"Product Deleted"); 
     } 

     sqlite3_close(companyDB); 
    } 

} 

@end 
+1

是否運行在iPhone模擬器這個代碼?如果是這樣,運行代碼沙箱(模擬如何將手機/ iPad上的),所以你必須有絕對路徑,/Users/user/Desktop/telecom.db,可能不是在模擬的應用程序實際上是在寫數據庫。所以你可能只是在看數據庫的兩個不同的副本。 – orpheist

+0

我在iOS模擬器上運行它。這很有趣。謝謝! – user3525783

回答

0

正如在評論中提到 - 當你的應用程序將在iOS上運行將沙盒。此外,如果你將包括SQLite數據庫與應用程序的資源文件 - 它會進入你的應用程序文件夾中/資源文件夾。並且該文件夾中的所有內容都不可寫。

所以如果你需要,你想讀/寫,你需要把它放在/文檔的應用程序文件夾文件夾裏面的數據庫。