2012-11-29 104 views
2

我有一個名爲data_items.sqlite的現有SQLite數據庫。這個數據庫包括約10桌,並且我想導入的XCode以這種方式打開與PhoneGap的插件數據庫的一些初始數據:將現有的SQLite數據庫導入iOS Phonegap應用程序

function onDeviceReady() { 
    var db = window.sqlitePlugin.openDatabase("data_items.sqlite", "1.0", "PhoneGap Demo", 200000); 
    ... 
} 

我如何導入數據文件?我必須在哪裏複製文件?

回答

2

首先創建數據庫爲u使用的名稱data_items.sqlite和刪除.sqlite擴展名。然後將這個數據庫拖放到xCode的資源目錄中,然後按照下一步操作

選擇添加文件框的選項會出現 選中複選框說'將項目複製到目標組的文件夾(如果需要)'並完成。

你會看到數據庫文件作爲資源目錄的子列表

對不起,我沒有足夠的信譽來這裏發表圖片

現在,按照這個數據庫文件複製到位置

鏈接

How to copy sqlite database when application is launched in iOS?

我AppDelegate.m

用下面的代碼在最後一行@end之前
- (void) copyDatabaseIfNeeded{ 
     NSFileManager *fileManager = [NSFileManager defaultManager]; 
     NSError *error; 
     _dbPath = [self getDBPath]; 

     BOOL success = [fileManager fileExistsAtPath:_dbPath]; 

    if(!success){ 
     NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"data_items"]; 
     success = [fileManager copyItemAtPath:defaultDBPath toPath:_dbPath error:&error]; 
     if (!success) 
      NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]); 
    }} 
/********* Database Path *********/ 
- (NSString *) getDBPath 
{ 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES); 
    NSString *documentsDir = [paths objectAtIndex:0]; 

    return [documentsDir stringByAppendingPathComponent:@"data_items"]; 
} 

和代碼

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions 
{ 
    CGRect screenBounds = [[UIScreen mainScreen] bounds]; 

這就是它之後的行

[self copyDatabaseIfNeeded]; 

複製。使用您的代碼

function onDeviceReady() { 
    var db = window.sqlitePlugin.openDatabase("data_items", "1.0", "data_items", 200000); 
    ... 
} 

您也可以從複製的位置直接編輯數據庫。使用以下鏈接

Where does the iPhone Simulator store its data?

相關問題