2014-05-16 32 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数据库与应用程序的资源文件 - 它会进入你的应用程序文件夹中/资源文件夹。并且该文件夹中的所有内容都不可写。

所以如果你需要,你想读/写,你需要把它放在/文档的应用程序文件夹文件夹里面的数据库。