我想在我的网络应用程序中使用预填充的数据库,以便我的应用程序可以离线运行。我如何使用最新版本的 PhoneGap/Cordova (2.0) 执行此操作?
我知道以前有人问过这个问题,但所有答案相对于当前版本的 cordova 和 iOS 似乎都已过时
https://github.com/atkinson/phonegap-prepopulate-db两年没更新了 https://github.com/davibe/Phonegap-SQLitePlugin 7个月没更新了,是1.7用的
我在这里找到了一个帖子: http://www.raymondcamden.com/index.cfm/2012/7/27/Guest-Blog-Post-Shipping-a-populated-SQLite-DB-with-PhoneGap这是唯一的方法吗?
此外,我应该注意我使用的是 iOS 6
最佳答案
我在 iOS6 上使用了以下适用于 Cordova 2.7 的插件,我看到它是一天前刚刚更新的。 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
if __has_feature(objc_arc)
NSURLCache* sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
else
NSURLCache* sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"] autorelease];
endif
[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 - 如何在 PhoneGap/Cordova 2.0 中使用预填充的 SQLite 数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13137555/