如何在PhoneGap / Cordova 2.0中使用预先填充的SQLite数据库?

我想与我的networking应用程序使用预先填充的数据库,以便我的应用程序脱机工作。 我怎样才能用最新版本的PhoneGap / Cordova(2.0)来做到这一点?

我知道这个问题之前已经被问过了,但是所有的答案似乎都与当前版本的cordova和iOS相比已经过时了

https://github.com/atkinson/phonegap-prepopulate-db两年来还没有更新https://github.com/davibe/Phonegap-SQLitePlugin尚未更新7个月,是1.7

我在这里find了一个post: http : //www.raymondcamden.com/index.cfm/2012/7/27/Guest-Blog-Post-Shipping-a-populated-SQLite-DB-with-PhoneGap这是唯一的方法?

另外,我应该注意到我使用iOS 6

我已经使用下面的插件Cordova 2.7与iOS6,我看到它刚刚更新了一天前。 https://github.com/pgsqlite/PG-SQLitePlugin-iOS

他们也有一个Android版本。 问题的一部分是PhoneGap的变化,所以经常保持插件更新可能是耗时的,所以他们失效。 这里是我在这个项目中使用的AppDelegate.m中的init方法的代码http://www.binpress.com/app/conference-core-ios-for-phonegap/1483

请记住,您有时需要擦除iOS模拟器,以便重新加载与应用程序相关的文件。

- (id)init{ NSHTTPCookieStorage* cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; [cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways]; int cacheSizeMemory = 8 * 1024 * 1024; // 8MB int cacheSizeDisk = 32 * 1024 * 1024; // 32MB 

如果__has_feature(objc_arc)

  NSURLCache* sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"]; 

其他

  NSURLCache* sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"] autorelease]; 

万一

 [NSURLCache setSharedURLCache:sharedCache]; 
 databaseName = @"SomeCoolDatabase.db"; NSLog(@"databaseName: %@", databaseName); databasePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex: 0]; NSLog(@"databasePath: %@", databasePath); databaseFile = [databasePath stringByAppendingPathComponent:databaseName]; NSLog(@"databaseFile: %@", databaseFile); // Execute the "checkAndCreateDatabase" function [self checkAndCreateDatabase]; self = [super init]; return self; 

}

– (void)checkAndCreateDatabase {

 // Check if the SQL database has already been saved to the users phone, if not then copy it over BOOL success; // Create a FileManager object, we will use this to check the status // of the database and to copy it over if required NSFileManager *fileManager = [NSFileManager defaultManager]; // Check if the database has already been created in the users filesystem success = [fileManager fileExistsAtPath:databaseFile]; // If the database already exists then return without doing anything if(success){ NSLog(@"Database Present"); return; } else { NSLog(@"database not present"); } // If not then proceed to copy the database from the application to the users filesystem // Get the path to the database in the application package NSString *databasePathFromApp = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:databaseName]; // Create the database folder structure [fileManager createDirectoryAtPath:databasePath withIntermediateDirectories:YES attributes:nil error:NULL]; // Copy the database from the package to the users filesystem [fileManager copyItemAtPath:databasePathFromApp toPath:databaseFile error:nil]; [fileManager release]; 

}

希望这可以帮助…

谢谢! 如果不从这里开始,我将无法做到这一点!

也就是说,这不是我开箱即用,所以我更新了它,而且我对于iOS的原生代码并不是很好 ,所以我非常乐于提供build议。

我将build议的代码添加到AppDelegate.m文件中,出现了一堆错误。 所以,我转移到AppDelegate.h文件:

 @interface AppDelegate : NSObject <UIApplicationDelegate>{} --Looks like this to start. 

使它看起来像这样:

 @interface AppDelegate : NSObject <UIApplicationDelegate>{ NSString* databaseName; NSString* databasePath; NSString* databaseFile; } 

我只需要在那里声明variables。 我也删除了这一行:

 [fileManager release]; 

正如根据这个答案,我的项目似乎自动释放? 我希望?

现在我必须努力获取数据库文件到正确的位置为Xcodefind它。

希望这可以帮助!

虽然这个问题已经过时了,但我认为它可能会帮助某人发布我的解决scheme – 发现于2017年。(由于PhoneGap已经改变,旧的解决scheme不再适用于Android和Apple应用程序市场,问题很长一段时间 – 问题是你想导入一个预填充的数据库 – 这不是很容易的解决scheme。 我发现最好,最简单 – 目前只是解决scheme – LiteHelpers CORDOVA-SQLITE-EXT( https://github.com/litehelpers/cordova-sqlite-ext

我已经添加到我的应用程序,它运作良好。 我不得不从PhoneGap切换,并使用CORDOVA – 但它为我工作。