在 Xcode 中,我试图让 FMDB 使用 SQLCipher 来加密数据库。在我的项目中,我已经有一个 SQLCipher 的编译版本,我已经证明它可以通过 sqlite3 调用工作。我有一个创建数据库和 1 个表然后插入一行的单元测试。除了数据库仍未加密外,一切都使用 FMDB。
-(id)initWithDatabaseFilename:(NSString*)filename{
NSString *databasePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]
stringByAppendingPathComponent: filename];
self.databasePath = databasePath;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:databasePath]) {
database = [FMDatabase databaseWithPath:databasePath];
[database setLogsErrors:YES];
[database setTraceExecution:NO];
BOOL keyCheck = [database setKey:@"B!GSecret"];
NSLog(@"Database is encrypted: %d",keyCheck);
NSLog(@"database created");
} else {
NSLog(@"Didnt need to create database");
}
[self createTable];
return self;
}
-(void)createTable{
BOOL tableExists = [self.database tableExists:kTASKTableName];
if(!tableExists){
[database open];
[database executeUpdate:@"CREATE TABLE TEST(TEST_PK integer primary key autoincrement, TITLE text, OTHERTITLE text, TESTVAL text, COMPLETED integer, CREATION_DATE double)"];
[database close];
}
}
-(BOOL) addTasks:(NSArray*) tasks{
BOOL insertSuccess = NO;
if([self.databasePath isEqualToString:@""]){
NSLog(@"Database has not yet been initialized");
}
[database open];
for(TESTOBJ *to in tasks){
[database executeUpdate:@"insert into TEST(TITLE, OTHERTITLE, TESTVAL) VALUES (?,?,?)",
to.title,to.otherTitle,to.testVal,nil];
}
[database close];
return insertSuccess;
}