Tableview app从sqlite数据库中删除

时间:2014-05-16 21:36:44

标签: ios objective-c sqlite

我有一个tableview应用程序,我试图删除存储在sqilte浏览器中的数据。当我删除模拟器中的数据时,关闭并重新运行模拟器后数据不会重新出现。但是,当我打开使用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 个答案:

答案 0 :(得分:0)

正如评论中提到的那样 - 当您的应用程序在iOS中运行时,它将被沙箱化。此外,如果您将sqlite数据库作为资源文件包含在您的应用程序中 - 它将进入应用程序文件夹中的/ Resources文件夹。并且该文件夹中的所有内容都不可写。

因此,如果您需要一个想要读/写的数据库,则需要将其放在应用程序文件夹中的/ Documents文件夹下。