I am using the database class for using the sqlite database
#import "DatabaseConnection.h"
@implementation DatabaseConnection
-(void)DBInitalize{
databaseName = @"sensorystimulation.sql";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
[self readFromDatabase];
}
-(NSMutableArray *)settingsData{
return settingsArray;
}
-(void)checkAndCreateDatabase{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
success = [fileManager fileExistsAtPath:databasePath];
if(success)
return;
NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName];
[fileManager copyItemAtPath:databasePathFromApp toPath:databasePath error:nil];
}
-(void)readFromDatabase{
settingsArray = [[NSMutableArray alloc] init];
if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) {
const char *sqlStatementNew = "my sql query";
sqlite3_stmt *compiledStatementNew;
if(sqlite3_prepare_v2(database, sqlStatementNew, -1, &compiledStatementNew, NULL) == SQLITE_OK) {
while(sqlite3_step(compiledStatementNew) == SQLITE_ROW) {
NSString *key_name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 0)];
NSString *key_value = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatementNew, 1)];
NSMutableDictionary *tempDic = [[NSMutableDictionary alloc] initWithObjectsAndKeys:key_name,@"key_name",key_value,@"key_value",nil];
[settingsArray addObject:tempDic];
[tempDic release];
}
sqlite3_finalize(compiledStatementNew);
}
}
}
-(void)updateSettings:(NSMutableArray *)values{
for (int l=0; l<[values count]; l++) {
NSString *key_name = [[values objectAtIndex:l] objectForKey:@"key_name"];
NSString *key_value = [[values objectAtIndex:l] objectForKey:@"key_value"];
sqlite3_stmt *updateStmt;
NSString *ts=[NSString stringWithFormat:@"UPDATE table key_value='%@' where key_name='%@'",key_value,key_name];
const char *sql = [ts cStringUsingEncoding:1];
if(sqlite3_prepare_v2(database, sql, -1, &updateStmt, NULL) != SQLITE_OK){
NSLog(@"Error while creating update statement. '%s'", sqlite3_errmsg(database));
}
if(SQLITE_DONE != sqlite3_step(updateStmt)){
NSLog(@"%@",ts);
NSLog(@"Error while updating. '%s'", sqlite3_errmsg(database));
}
}
}
-(void)quitApp{
sqlite3_close(database);
}
@end
and calling its object like this
initilize
databaseConnection = [[DatabaseConnection alloc] init];
[databaseConnection DBInitalize];
Update DB
NSMutableArray *valueArray = [[NSMutableArray alloc] init];
[valueArray addObject:[[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"abc",@"key_name",[NSString stringWithFormat:@"%d",abc],@"key_value",nil] autorelease]];
[valueArray addObject:[[[NSMutableDictionary alloc] initWithObjectsAndKeys:@"xyz",@"key_name",[NSString stringWithFormat:@"%d",xyz],@"key_value",nil] autorelease]];
[databaseConnection updateSettings:valueArray];
[valueArray release];
It works fine. not problem is using
But after a lot of updation approximately 100-200 times the following log(error) comes.. and after it everytime this error occur and I am not able to update DB. after this I have to quit application then I again it works ok
Error while updating. 'unable to open database file'
and due to this on of my other functionality also not work after error occur that tab on image view
Any idea regard this. please help.
-Amit Battan