Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to create SQLite3 database with file protection in iOS

Background: I have been watching through WWDC 2011 - 208 Securing iOS application. They mention about how to secure our data with file encryption called NSFileProtection. While I discover the automatic method mentioned (@38:00) is bugged, I hope the manual way (@37:00) is not. I tried using file protection with images and all is well. Only Sqlite I cannot create.

Problem: I try to create SQLite3 database (without core data) with sqlite3_open_v2 and passing SQLITE_OPEN_FILEPROTECTION_COMPLETEas the flag for the third argument. It does not return SQLITE_OK.

Code:

if (sqlite3_open_v2([databasePath UTF8String], &database, SQLITE_OPEN_FILEPROTECTION_COMPLETE, NULL) == SQLITE_OK){
   //everything works
}else{
  //failed
}

Update:
As borrrden mentioned, both has resulted in the following error code: SQLITE_MISUSE (Library used incorrectly)


Update 2: borrrden's 2nd comment was correct to point out the need for both flags. It works following the code:

if (sqlite3_open_v2([databasePath UTF8String], &database, SQLITE_OPEN_CREATE|SQLITE_OPEN_READWRITE|SQLITE_OPEN_FILEPROTECTION_COMPLETE, NULL) == SQLITE_OK)
like image 469
Byte Avatar asked Nov 04 '22 05:11

Byte


1 Answers

Sqlite is not happy about the lack of a file access mode being specified in your flags. It doesn't know whether it should open it writable or not, or whether or not to create it if it does not exist. Therefore, add the following two flags along with the one you are currently using:

SQLITE_OPEN_READWRITE
SQLITE_OPEN_CREATE

This signals that sqlite should open a writable database, and create it if it does not exist.

like image 149
borrrden Avatar answered Nov 15 '22 06:11

borrrden