mardi 7 avril 2015

sqlite database works fine and suddenly shows cannot open database error

hi i am creating a database app which includes Sqlite, everything works fine and suddenly after 20-30 transactions database not found error occurs, help me



-(void)open
{
BOOL success;
NSError *error;

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *databasePath = [documentsDirectory stringByAppendingPathComponent:@"Mohan.sqlite"];
// _DBPath=[[NSBundle mainBundle] pathForResource:@"Mohan" ofType:@"sqlite"];
//_DBPath=[NSString stringWithFormat:@"/var/mobile/Applications/C6088ECC-17F3-4A86-814C-7D816B7A49BB/Documents/Mohan.sqlite"];


NSLog(@"Database path: %@",databasePath);

NSFileManager *fileManager=[NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];

if (success)
{
_DBPath=databasePath;
}
else
{
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Mohan.sqlite"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:databasePath error:&error];
_DBPath=defaultDBPath;
if (!success)
{
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}

}


-(void)insertValue:(NSString *)query
{
sqlite3_stmt *statement;

[self open];
if (sqlite3_open([_DBPath UTF8String], &_DB) == SQLITE_OK)
{

if (sqlite3_prepare_v2(_DB, [query UTF8String], -1, &statement, NULL)==SQLITE_OK)
{

if (sqlite3_step(statement) == SQLITE_DONE)
{
NSLog(@"Data Inserted");
NSLog(@"%@",query);

}
else
{
NSLog(@"Failed to Insert");
}

}

}
NSLog(@"ERROR MSG: %@", [NSString stringWithUTF8String:(char*)sqlite3_errmsg(_DB)]);
NSLog(@"Last inserted row id: %lld",sqlite3_last_insert_rowid(_DB));
sqlite3_finalize(statement);

sqlite3_close(_DB);
NSLog(@"db closed");

}


the error occurs in insert value and shows unable to open database


Aucun commentaire:

Enregistrer un commentaire