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] initWithObjectsAndKe开发者_开发百科ys:@"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
1) Did you have a look at these two questions:
unable to open database
Sqlite Opening Error : Unable to open database
You might see similar problems as reported in the above questions.
It appears that you are missing a sqlite3_finalize in "updateSettings".
Are you closing the DB every time you release "databaseConnection"?
2)
Are you opening the DB 100-200 times?
If so, maybe you should think about if this is the best approach for your App.
Have you thought of using a Singleton for the DB access? -> Open the DB once during init and then re-use the connection every time. This might not just speed up your App but also reduce the memory usage. Check out:
http://cocoawithlove.com/2008/11/singletons-appdelegates-and-top-level.html
In your decision on this, you should consider if in your App multiple threads access the DB at the same time.
Best,
Ralph
精彩评论