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

Ken*_*nci 6 sqlite web-applications ios cordova

我想在我的网络应用程序中使用预先填充的数据库,以便我的应用程序脱机工作.我怎样才能使用最新版本的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

小智 2

我在 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
Run Code Online (Sandbox Code Playgroud)

如果 __has_feature(objc_arc)

    NSURLCache* sharedCache = [[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"];
Run Code Online (Sandbox Code Playgroud)

别的

    NSURLCache* sharedCache = [[[NSURLCache alloc] initWithMemoryCapacity:cacheSizeMemory diskCapacity:cacheSizeDisk diskPath:@"nsurlcache"] autorelease];
Run Code Online (Sandbox Code Playgroud)

万一

[NSURLCache setSharedURLCache:sharedCache];
Run Code Online (Sandbox Code Playgroud)
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;
Run Code Online (Sandbox Code Playgroud)

}

-(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];
Run Code Online (Sandbox Code Playgroud)

}

希望这可以帮助...