Hi all,
In my cocoa application I am maintaining a SQLite db within resources folder and trying to do some select, delete operations in it but after some time it starts giving me 'Database is locked' error.
The methods which I am using for select delete operations are as follows:
// method to retrieve data
if (sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK)
{
sqlite3_close(database);
NSAssert(0, @"Failed to open database");
}
NSLog(@"mailBodyFor:%d andFlag:%d andFlag:%@",UId,x,Ffolder);
NSMutableArray *recordsToReturn = [[NSMutableArray alloc] initWithCapacity:2];
NSString *tempMsg;
const char *sqlStatementNew;
NSLog(@"before switch");
switch (x) {
case 9:
// tempMsg=[NSString stringWithFormat:@"SELECT * FROM users_messages"];
tempMsg=[NSString stringWithFormat:@"SELECT message,AttachFileOriName as oriFileName,AttachmentFileName as fileName FROM users_messages WHERE id = (select message_id from users_messages_status where id= '%d')",UId];
NSLog(@"mail body query - %@",tempMsg);
break;
default:
break;
}
sqlStatementNew = [tempMsg cStringUsingEncoding:NSUTF8StringEncoding];
sqlite3_stmt *compiledStatementNew;
NSLog(@"before if statement");
if(sqlite3_prepare_v2(database, sqlStatementNew, -1, &compiledStatementNew, NULL) == SQLITE_OK) {
NSLog(@"the sql is finalized");
while(sqlite3_step(compiledStatementNew) == SQLITE_ROW) {
NSMutableDictionary *recordDict = [[NSMutableDictionary alloc] initWithCapacity:3];
NSString *message;
if((char *)sqlite3_column_text(compiledStatementNew, 0)){
message = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 0)];
}
else{
message = @"";
}
NSLog(@"message - %@",message);
NSString *oriFileName;
if((char *)sqlite3_column_text(compiledStatementNew, 1)){
oriFileName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 1)];
}
else{
oriFileName = @"";
}
NSLog(@"oriFileName - %@",oriFileName);
NSString *fileName;
if((char *)sqlite3_column_text(compiledStatementNew, 2)){
fileName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 2)];
}
else{
fileName = @"";
}
NSLog(@"fileName - %@",fileName);
[recordDict setObject:message forKey:@"message"];
[recordDict setObject:oriFileName forKey:@"oriFileName"];
[recordDict setObject:fileName forKey:@"fileName"];
[recordsToReturn addObject:recordDict];
[recordDict release];
}
sqlite3_finalize(compiledStatementNew);
sqlite3_close(database);
NSLog(@"user messages return -%@",recordsToReturn);
return recordsToReturn;
}
else{
NSLog(@"Error while creating retrieving mailBodyFor in messaging '%s'", sqlite3_errmsg(database));
sqlite3_close(database);
}
// method to delete data
if (sqlite3_open([databasePath UTF8String], &database) != SQLITE_OK)
{
sqlite3_close(database);
NSAssert(0, @"Failed to open database");
}
NSString *deleteQuery = [[NSString alloc] initWithFormat:@"delete from users_messages_status where id IN(%@)",ids];
NSLog(@"users_messages_status msg deleteQuery - %@",deleteQuery);
sqlite3_stmt *deleteStmnt;
const char *sql = [deleteQuery cStringUsingEncoding:NSUTF8StringEncoding];
if(sqlite3_prepare_v2(database, sql, -1, &deleteStmnt, NULL) != SQLITE_OK){
NSLog(@"Error while creating delete statement. '%s'", sqlite3_errmsg(database));
}
else{
NSLog(@"successful deletion from users_messages");
}
if(SQLITE_DONE != sqlite3_step(deleteStmnt)){
NSLog(@"Error while deleting. '%s'", sqlite3_errmsg(database));
}
sqlite3_close(database);
Things are going wrong in this sequence
Data is retrieved
'Database is locked' error arises on performing delete operation.
When I retry to perform 1st step.. it now gives same error.
Can anyone suggest me:
If I am doing anything wrong or missing some check?
Is there any way to unlock it when it gives locked error?
Thanks,
Miraaj