我的代码例如
- (BOOL)openDB:(NSString*)filename
{
DBPathString =filename;
sqlite3_stmt *statement;
if (sqlite3_open([DBPathString UTF8String], &database) == SQLITE_OK) {
NSString *password = [[NSUserDefaults standardUserDefaults] objectForKey:@"Password"];
const char* key = [password UTF8String];
sqlite3_key(database, key, strlen(key));
NSString *querySql = [NSString stringWithFormat:@"SELECT sum(value) , income_date, count(income_date) FROM incomes WHERE income_date between '1990-01-01' and '2014-01-01' GROUP BY income_date" ];
const char *query_sql = [querySql UTF8String];
if (sqlite3_prepare(database, query_sql, -1, &statement, NULL) == SQLITE_OK) {
while (sqlite3_step(statement) == SQLITE_ROW) {
float value = sqlite3_column_double(statement, 0);
NSString* date = [[NSString alloc] initWithUTF8String:(const char *)sqlite3_column_text(statement, 1)];
int count = sqlite3_column_int(statement, 2);
NSMutableDictionary *daysThingsDataDetail = [[NSMutableDictionary alloc] init];
[daysThingsDataDetail setObject:[NSNumber numberWithFloat:value] forKey:@"value"];
[daysThingsDataDetail setObject:[NSNumber numberWithInt:count] forKey:@"count"];
[daysThingsData setObject:daysThingsDataDetail forKey:date];
//NSLog(@"%g", value);
}
}
} else {
NSLog(@"failed opened DB %s ", sqlite3_errmsg(database));
sqlite3_close(database);
return NO;
}
sqlite3_close(database);
return YES;
}
要求测试
NSString * Filename = [NSString stringWithFormat:@"test.sqlite3"];
NSString *DBPathString = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:Filename];
BOOL result = YES;
int count = 0;
while (result) {
result = [self openDB:DBPathString];
count++;
NSLog(@"%d", count);
}
2013-08-08 15:11:27.023 xxx[16407:c07] 2534 2013-08-08 15:11:27.023 xxx[16407:c07] 失败打开数据库无法打开数据库文件 2013-08-08 15:11: 27.024 xxx[16407:c07] 2535