update: 2011/09/26
A. 準備好已儲存資料的 Core Data sqlite 檔案.
方法 1:
直接在模擬器的 UI 操作介面中先建好資料, 然後到 iPhone Simulator Directory 裡,
將 app Documents 目錄下的 xxx.sqlite 檔 copy 出來.
說明: 可先建立 iPhone Simulator Directory 的 symbolic link, 方便使用.
方法 2:
在實機測試時, 輸入想要預設儲存的資料, 然後配合:
利用 iTunes 備份與回復 Core Data 資料, 將 xxx.sqlite 檔 copy 出來.
方法 3: (未試過)
藉由 方法 1 或 方法 2, copy 出不含資料的空白資料庫, 然後利用:
SQLite database browser 工具, 匯入 CSV 格式的資料到 xxx.sqlite 檔.
--------------------------------------------------------------------------------------------------
B. 將準備好的 xxx.sql 檔案加到專案裡:
先新增一個 Resources 的 Group, 然後再將 xxx.sqlite 檔加進來.C. 開啟 Lala_s_Program_NoteAppDelegate.m 檔, 修改如下:
/**
Returns the persistent store coordinator for the application.
If the coordinator doesn't already exist, it is created and the application's store added to it.
*/
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (__persistentStoreCoordinator != nil)
{
return __persistentStoreCoordinator;
}
/*
//@add: import prepopulated sqlite data to a coredata db
*/
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
// a. 要存放預設資料的位置, 即 app 在執行時會讀取 .sql 的地方
NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"Lala_s_Program_Note.sqlite"];
//NSLog(@"writableDBPath %@", writableDBPath);
/*
writableDBPath
Simulator: /Users/lanli/Library/Application Support/iPhone Simulator/4.3.2/Applications/8F85FBE9-9C82-4DCD-84D5-C68173657E4C/Documents/Lala_s_Program_Note.sqlite
iDevice: /var/mobile/Applications/C3F2AB7F-A296-4E9D-96C9-554E9499E1D4/Documents/Lala_s_Program_Note.sqlite
*/
// b. 一開始加到專案裡的 .sql 位置, 直接就在 app 的目錄下
NSString *defaultStorePath = [[NSBundle mainBundle] pathForResource:@"Lala_s_Program_Note" ofType:@"sqlite"];
//NSLog(@"defalultStorePath %@", defaultStorePath);
/*
defalultStorePath
Simulator: /Users/lanli/Library/Application Support/iPhone Simulator/4.3.2/Applications/8F85FBE9-9C82-4DCD-84D5-C68173657E4C/Lala's Program Note.app/Lala_s_Program_Note.sqlite
iDevice: /var/mobile/Applications/C3F2AB7F-A296-4E9D-96C9-554E9499E1D4/Lala's Program Note.app/Lala_s_Program_Note.sqlite
*/
// c. app 在執行時讀取 .sql 的 URL地方
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Lala_s_Program_Note.sqlite"];
//NSLog(@"store URL %@", storeURL);
/*
store URL
Simulator: file://localhost/Users/lanli/Library/Application%20Support/iPhone%20Simulator/4.3.2/Applications/8F85FBE9-9C82-4DCD-84D5-C68173657E4C/Documents/Lala_s_Program_Note.sqlite
iDevice: file://localhost/var/mobile/Applications/C3F2AB7F-A296-4E9D-96C9-554E9499E1D4/Documents/Lala_s_Program_Note.sqlite
*/
// Put down default db if it doesn't already exist
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:writableDBPath]) {
if (defaultStorePath) {
[fileManager copyItemAtPath:defaultStorePath toPath:writableDBPath error:NULL];
}
}
//@original:
//NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Lala_s_Program_Note.sqlite"];
NSError *error = nil;
__persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
if (![__persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
{
. . . .
//NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
//abort();
//@update
[Common showCoreDataError];
}
return __persistentStoreCoordinator;
}
--------------------------------------------------------------------------------------------------
D. 結果圖:
請問如果要在sql加入image 建議怎做?
回覆刪除你好:
回覆刪除不好意思, 已有一段時間沒有在開發 iOS 了.
先前使用 Core Data 來代替直接使用 sqlite,
目前, 覺得 Core Data 的操作, 雖然較高階化, 但是除錯不易.
所以, 之後有使用到的話, 可能會用 sqlite 來儲存資料.
接著, 回答你的問題:
由於我沒有實際去測試, 僅提供二個參考網址:
(順便可練習一下英文)
http://stackoverflow.com/questions/5207871/get-image-from-sql-in-ios
http://www.iphonesdkarticles.com/2009/02/sqlite-tutorial-saving-images-in.html
感謝~~
刪除