Ios Tableview应用程序从sqlite数据库中删除
我有一个tableview应用程序,试图删除存储在sqilte浏览器中的数据。当我删除模拟器中的数据时,关闭并重新运行模拟器后,数据不会重新出现。但是,当我使用sqlite浏览器打开保存原始数据的数据库时,删除的数据仍然显示在sqlite浏览器中。有人能告诉我我遗漏了什么吗Ios Tableview应用程序从sqlite数据库中删除,ios,objective-c,sqlite,Ios,Objective C,Sqlite,我有一个tableview应用程序,试图删除存储在sqilte浏览器中的数据。当我删除模拟器中的数据时,关闭并重新运行模拟器后,数据不会重新出现。但是,当我使用sqlite浏览器打开保存原始数据的数据库时,删除的数据仍然显示在sqlite浏览器中。有人能告诉我我遗漏了什么吗 #import "DataAccess.h" #import "sqlite3.h" #import "Product.h" #import "Company.h" @implementation DataAccess
#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
正如评论中所提到的,当你的应用程序在iOS中运行时,它将被沙盒。此外,如果将sqlite数据库作为资源文件包含在应用程序中,它将进入应用程序文件夹中的/Resources文件夹。文件夹中的所有内容都不可写
所以,如果您需要一个要读/写的数据库,您需要将它放在应用程序文件夹中的/Documents文件夹下 您是否在iOS模拟器中运行此代码?如果是这样,代码将运行沙盒(以模拟它在手机/iPad上的运行方式),因此您在那里的绝对路径/Users/user/Desktop/telecom.db可能不是模拟应用程序实际写入数据库的位置。因此,您可能只是在查看数据库的两个不同副本。我正在iOS模拟器上运行它。那很有趣。谢谢