首先創建數據庫爲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?
來源
2013-12-02 12:37:56
Avi