Ios 重新安装应用程序时内存不足
我绝望了 我使用sqlite创建了一个应用程序,在我向苹果商店提交更新之前,它一直运行良好。 当我的应用程序重新安装,我试图打开一个数据库连接,我得到了一个内存不足的问题 我尝试了很多方法来解决这个问题,但直到今天我还是无法解决这个问题 我使用以下代码打开db的连接:Ios 重新安装应用程序时内存不足,ios,objective-c,sqlite,out-of-memory,Ios,Objective C,Sqlite,Out Of Memory,我绝望了 我使用sqlite创建了一个应用程序,在我向苹果商店提交更新之前,它一直运行良好。 当我的应用程序重新安装,我试图打开一个数据库连接,我得到了一个内存不足的问题 我尝试了很多方法来解决这个问题,但直到今天我还是无法解决这个问题 我使用以下代码打开db的连接: -(Boolean)openConectionSQLiteDb { if (sqlite3_open([[GeneralFunction getInfoFromUserDefault:@"dbpath"] UTF8
-(Boolean)openConectionSQLiteDb
{
if (sqlite3_open([[GeneralFunction getInfoFromUserDefault:@"dbpath"] UTF8String], &db) != SQLITE_OK)
{
NSLog(@"Failed to open database!");
NSLog(@"%s",sqlite3_errmsg(statement));
//[GeneralFunction setDb:[[DbInteraction alloc] init:[GeneralFunction getDbName]]];
{
NSLog(@"Failed to open database! >>%@",[GeneralFunction getInfoFromUserDefault:@"dbpath"]);
// NSLog(@"222%s",sqlite3_errmsg(statement));
}
sqlite3_finalize(statement);
return false;
}
else
{
sqlite3_exec(db, "PRAGMA foreign_keys = on", NULL, NULL, NULL);
return true;
}
return false;
}
下面的代码用于在数据库中插入日期:
-(Boolean)insertOrReplace:(NSDictionary *)elementoToInsert inTable:(NSString *)tableName
{
if([self openConectionSQLiteDb])
{
NSString * insert = [self createInsertStringWith:elementoToInsert in:tableName];
insert = [insert stringByReplacingOccurrencesOfString:@"INSERT" withString:@"INSERT OR REPLACE "];
const char *insert_stmt = [insert UTF8String];
sqlite3_prepare_v2(db, insert_stmt,
-1, &statement, NULL);
if (sqlite3_step(statement) == SQLITE_DONE)
{
sqlite3_finalize(statement);
sqlite3_close(db);
// NSLog(@"FECHOU");
return true;
}
NSLog(@"INSERT ERROR %s",sqlite3_errmsg(db));
sqlite3_finalize(statement);
}
sqlite3_close(db);
NSLog(@"FECHOU");
return false;
}
我还将在必要时使用以下代码将Db文件复制到可写路径:
-(id)init:(NSString *) dbName
{
self = [super init];
nomeDb = dbName;
BOOL success;
NSFileManager *filemngr = [[NSFileManager alloc] init];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *documentDirectory = [paths objectAtIndex:0];
NSString *writableDbPath = [documentDirectory stringByAppendingPathComponent:nomeDb];
// NSLog(@"%@",[GeneralFunction getInfoFromUserDefault:@"dbpath"]);
NSString * lastPath =[GeneralFunction getInfoFromUserDefault:@"dbpath"];
success = [filemngr fileExistsAtPath:lastPath];
if(!success)
{
NSLog(@"Arquivo não existe nesse Directorio >%@",lastPath);
success = [filemngr fileExistsAtPath:writableDbPath];
if(!success)
{
NSLog(@"Arquivo não existe nesse Directorio >%@",writableDbPath);
NSString *defaultDbPath = [[[NSBundle mainBundle] resourcePath]stringByAppendingPathComponent:nomeDb];
success = [filemngr copyItemAtPath:defaultDbPath toPath:writableDbPath error:&error];
NSLog(@"Copiou Para :%@",writableDbPath);
if (!success)
{
NSLog(@"Arquivo não existe nesse Directorio pela seguanda vez >%@",writableDbPath);
//[status setText:[error localizedDescription]];
/* UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error!"
message:[error localizedDescription]
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];*/
}
else
{
NSLog(@"Achou 2");
NSMutableDictionary * dic = [[NSMutableDictionary alloc] init];
// NSLog(@"%@",writableDbPath);
[dic setObject:writableDbPath forKey:@"dbpath"];
[GeneralFunction saveTeacherInfo:dic];
writablePath = writableDbPath;
}
}
else
{
NSLog(@"Banco esta em %@",writableDbPath);
}
}
else{
NSLog(@"Achou");
}
// [bd executeUpdate:@"PRAGMA foreign_keys=ON"];
return self;
}
因此,当我第一次安装应用程序时,它运行良好。但是,如果我再次运行应用程序,当我提交更新时也会发生这种情况,因为我得到了日志错误,无法打开内存不足的数据库
谁来帮帮我,我快被炒鱿鱼了
我为我糟糕的英语感到抱歉;Dsqlite有时会错误地报告内存不足错误。通常,如果这发生在应用程序更新之后,那是因为您的sqlite访问代码中有一个bug。如果您的数据库模式在应用程序版本之间发生了更改,但您没有确保数据库调用向后兼容,则经常会看到此错误
如果在创建失败时,CREATE TABLE语句后面没有ALTER TABLE语句来加快模式的创建速度,因为表已经存在,那么就有可能进行不向后兼容的数据库调用。是的,伙计们,我终于可以解决我的问题了。
这是因为当我检查我的可写路径中是否已经复制了数据库时,在真实情况下,我忘记了更新引用数据库的内部路径。因此,当我尝试打开连接时,它会返回错误,因为我在错误的位置查找数据库。您需要在应用程序包中添加数据库文件,然后从[[NSBundle mainBundle]URLForResource:方法加载数据库。请确认。失败时的“dbpath”是什么?它是我在库文件夹中复制数据库的目录。Kampai,是的,伙计,我正在这样做。我只是在帖子中添加了我用来初始化数据库的代码,并在必要时复制它。如果你们都能看一下的话。这会很好,但我不会更改它数据库结构上的任何东西。在我不知道这是否是制造差异,但我使用firefox插件创建数据库并导入到我的项目包中。