some useful command:
begin transaction;
insert into ..your sql statement
if(some error occors) {
rollback transaction;
}else {
end transaction; //or commit
}
#import <Foundation/Foundation.h>
#import <sqlite3.h>
@interface SqliteCrypter : NSObject {
@private
NSString* cstrSourceDataBase;
NSString* cstrDestDataBase;
}
-(BOOL)encryptDatabase: (NSString*)acstrPassword cipher: (NSString*)acstrCipher iteration:(NSString*)acstrIteration;
-(sqlite3*)decryptDatabase: (NSString*)acstrPassword cipher: (NSString*)acstrCipher iteration:(NSString*)acstrIteration;
-(BOOL)changePasswordFrom: (NSString*)acstrOlderPassword: toNewPassword: (NSString*)acstrNewPassword;
@end
#import “SqliteCrypter.h”
@interface SqliteCrypter(Private)
-(BOOL)copyDataTo: (sqlite3*)apSqliteTo;
@end
@implementation SqliteCrypter
– (void) dealloc
{
[cstrDestDataBase release]; cstrDestDataBase = nil;
[cstrSourceDataBase release]; cstrSourceDataBase = nil;
[super dealloc];
}
-(BOOL)encryptDatabase: (NSString*)acstrPassword cipher: (NSString*)acstrCipher iteration:(NSString*)acstrIteration {
NSString* docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[cstrDestDataBase release], cstrDestDataBase = nil;
cstrDestDataBase = [[NSString stringWithFormat:@”%@/crypt.sqlite”, docPath] copy];
BOOL bSuccess = YES;
sqlite3* psqlite3Crypted = NULL;
if(sqlite3_open_v2([cstrDestDataBase UTF8String], &psqlite3Crypted, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL ) != SQLITE_OK) {
fprintf(stderr, “encryptDatabase error:%s\n”, sqlite3_errmsg(psqlite3Crypted));
bSuccess = NO;
return bSuccess;
}
// submit the password
const char *pzcharKey = [acstrPassword UTF8String];
int iStatus = -1;
iStatus = sqlite3_key(psqlite3Crypted, pzcharKey, strlen(pzcharKey));
if(acstrCipher) {
iStatus = sqlite3_exec(psqlite3Crypted, (const char*)[[NSString stringWithFormat:@”PRAGMA cipher=’%@’;”, acstrCipher] UTF8String], NULL, NULL, NULL);
}
if(acstrIteration) {
iStatus = sqlite3_exec(psqlite3Crypted, (const char*)[[NSString stringWithFormat:@”PRAGMA kdf_iter=’%@’;”, acstrIteration] UTF8String], NULL, NULL, NULL);
}
[self copyDataTo: psqlite3Crypted];
return bSuccess;
}
-(sqlite3*)decryptDatabase: (NSString*)acstrPassword cipher: (NSString*)acstrCipher iteration:(NSString*)acstrIteration {
int iStatus = -1;
sqlite3* psSqlite3 = NULL;
iStatus = sqlite3_open_v2([cstrDestDataBase UTF8String], &psSqlite3, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL );
if (iStatus != SQLITE_OK) {
fprintf(stderr, “decryptDatabase can’t open database!”);
return psSqlite3;
}
const char *pzcharKey = [acstrPassword UTF8String];
iStatus = sqlite3_key(psSqlite3, pzcharKey, strlen(pzcharKey));
if (iStatus != SQLITE_OK) {
fprintf(stderr, “database should be opened firstly.\n”);
return psSqlite3;
}
if(acstrCipher) {
iStatus = sqlite3_exec(psSqlite3, (const char*)[[NSString stringWithFormat:@”PRAGMA cipher=’%@’;”, acstrCipher] UTF8String], NULL, NULL, NULL);
}
if(acstrIteration) {
iStatus = sqlite3_exec(psSqlite3, (const char*)[[NSString stringWithFormat:@”PRAGMA kdf_iter=’%@’;”, acstrIteration] UTF8String], NULL, NULL, NULL);
}
//test
const char* pcharGetRestaurantSql = “select * from restaurant limit 0,2;”;
sqlite3_stmt* pstmt = NULL;
int iStatusCode = sqlite3_prepare(psSqlite3, pcharGetRestaurantSql, -1, &pstmt, NULL);
if (iStatusCode == SQLITE_OK) {
iStatusCode = sqlite3_step(pstmt);
while (iStatusCode == SQLITE_ROW) {
int iId = sqlite3_column_int(pstmt, 0);
printf(“id is %d\n”, iId);
iStatusCode = sqlite3_step(pstmt);
}
}
return psSqlite3;
}
-(BOOL)changePasswordFrom: (NSString*)acstrOlderPassword: toNewPassword: (NSString*)acstrNewPassword {
BOOL bSuccess = YES;
int iStatus = -1;
sqlite3* psSqlite3 = NULL;
iStatus = sqlite3_open_v2([cstrDestDataBase UTF8String], &psSqlite3, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL );
if (iStatus != SQLITE_OK) {
fprintf(stderr, “decryptDatabase can’t open database!”);
bSuccess = NO;
}
const char *pzcharKey = [acstrOlderPassword UTF8String];
iStatus = sqlite3_key(psSqlite3, pzcharKey, strlen(pzcharKey));
if (iStatus != SQLITE_OK) {
fprintf(stderr, “old password is error.\n”);
bSuccess = NO;
}
const char* pzcharNewPassworld = [acstrNewPassword UTF8String];
iStatus = sqlite3_rekey(psSqlite3, pzcharNewPassworld, strlen(pzcharNewPassworld));
if (iStatus != SQLITE_OK) {
fprintf(stderr, “change to new password error.\n”);
bSuccess = NO;
}
sqlite3_close(psSqlite3);
return bSuccess;
}
-(BOOL)copyDataTo: (sqlite3*)apSqliteTo {
BOOL bSuccess = YES;
NSString* cstrSqlDump = [NSString stringWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@”c” ofType:@”dump”] encoding:NSUTF8StringEncoding error:nil];
if (cstrSqlDump != nil) {
char* pzDumpSql = (char*)[cstrSqlDump UTF8String];
char* pcError = NULL;
int iStatus = sqlite3_exec(apSqliteTo, pzDumpSql, NULL, NULL, &pcError);
if (iStatus != SQLITE_OK) {
fprintf(stderr, “error copyDataFrom %s”, sqlite3_errmsg(apSqliteTo));
bSuccess = NO;
}else {
NSLog(@”encrypt database successfully!”);
}
sqlite3_close(apSqliteTo);
}
return bSuccess;
}
@end
here is the source code: before your run the project, you must do download the openssl and the sqlite cipher source
https://skydrive.live.com/?cid=0fd1e8e318234b14&sc=documents&uc=1&id=FD1E8E318234B14%21251#











