Đang chuẩn bị liên kết để tải về tài liệu:
Phát triển ứng dụng cho iPhone và iPad - part 8
Đang chuẩn bị nút TẢI XUỐNG, xin hãy chờ
Tải xuống
Tham khảo tài liệu 'phát triển ứng dụng cho iphone và ipad - part 8', 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 39 You will populate this variable in the initializeDatabase function. Then every other function in your class will have access to the database. Now you ll create the init function that is used by callers to initialize instances of this class. In init you will make an internal call to initialize the database. The init function should look like this - id init dnwadnadnn Call super init to invoke superclass initiation code if self super init set the reference to the database self initializeDatabase return self DBAccess.m Your initializeDatabase function will do just that. It will go out and get the path to the database and attempt to open it. Here is the code for initializeDatabase Available for download on Wrax.cnm Open the database connection - void initializeDatabase Get the database from the application bundle. NSString path NSBundle mainBundle pathForResource catalog ofType @ db Open the database. if sqlite3_open path UTF8String database SQLITE_OK NSLog Opening Database else Call close to properly clean up sqlite3_close database NSAssert1 0 Failed to open database s . sqlite3_errmsg database DBAccess.m You can see that you need the path to the database file. Because you put the catalog.db file in the Resources folder the database will get deployed to the device in the main bundle of the application. There is a handy class NSBundle for getting information about an application bundle. The mainBundle method returns a reference to the main application bundle and the 40 I CHAPTER 2 THE IPHONE AND IPAD DATABASE SQLITE pathForResource ofType method returns the path to the specified file in the bundle. Because you specified the resource as catalog and the type as db the method will return the path to catalog.db. Next you use the C function sqlite3_open to open a connection to the database. You pass in the path to the database and the address to a variable of type sqlite3 . The second parameter will be populated with the handle to the .