I am using a database class to use 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 as follows
initialize
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 great. the problem is not to use
But after a large number of updates about 100-200 times the following log appears (error) .. and after it every time this error occurs, and I can not update the database. after that I have to exit the application, then I am working fine again
Error while updating. 'unable to open database file'
and because of this, according to my other functionality, they also do not work after an error occurs, that the tab is in the form of an image
Any idea consider this. please, help.
-Show buttan