J'utilise la classe de base de données pour l'utilisation de la base de données SQLiteiphone sqlite 'Impossible d'ouvrir le fichier de base de données' quelque temps
#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
et appeler son objet comme celui-ci
initilize
databaseConnection = [[DatabaseConnection alloc] init];
[databaseConnection DBInitalize];
Mise à jour 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 wo Très bien. pas de problème utilise
Mais après beaucoup de mise à jour environ 100-200 fois le journal suivant (erreur) vient .. et après chaque fois que cette erreur se produit et je ne suis pas capable de mettre à jour DB. après cela, je dois quitter l'application puis je encore, il fonctionne bien
Error while updating. 'unable to open database file'
et en raison de cela sur mon autre fonctionnalité fonctionne pas non plus après une erreur se produit cet onglet sur la vue de l'image
Tout ce qui concerne idée cela. aidez s'il vous plaît.
-Amit Battan
Too..Much..Code .. –
oui qui est un code complet de la classe de db .. fonction - (void) updateSettings: valeurs (NSMutableArray *) a un problème –