Tham khảo tài liệu 'phát triển ứng dụng cho iphone và ipad - part 9', công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả | Connecting to Your Database 49 each time but parameterized queries offer the performance advantage of preparing and compiling the statement once and the statement being cached for reuse. Writing to the Database If you modify the sample application or create your own SQLite application that attempts to write to the database you will have a problem. The version of the database that you are using in the sample code is located in the application bundle but the application bundle is read-only so attempting to write to this database will result in an error. To be able to write to the database you need to make an editable copy. On the iPhone this editable copy should be placed in the documents directory. Each application on the iPhone is sandboxed and has access only to its own documents directory. The following code snippet shows how to check to see if a writable database already exists and if not create an editable copy. Create a writable copy of the default database from the bundle in the application Documents directory. - void createEditableDatabase Check to see if editable database already exists BOOL success NSFileManager fileManager NSFileManager defaultManager NSError error NSArray paths NSSearchPathForDirectoriesInDomains NSDocumentDirectory NSUserDomainMask YES NSString documentsDir paths objectAtIndex 0 NSString writableDB documentsDir stringByAppendingPathComponent @ success fileManager fileExistsAtPath writableDB The editable database already exists if success return The editable database does not exist Copy the default DB to the application Documents directory. NSString defaultPath NSBundle mainBundle resourcePath stringByAppendingPathComponent @ success fileManager copyItemAtPath defaultPath toPath writableDB error error if success NSAssert1 0 Failed to create writable database file . error localizedDescription You would then need to change your database access code to call this function and then refer to the editable copy