如何使用PhoneGap / Cordova 2.0预填的SQLite数据库? [英] How to use a prepopulated SQLite database with PhoneGap / Cordova 2.0?

查看:75
本文介绍了如何使用PhoneGap / Cordova 2.0预填的SQLite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用预先填充的数据库与我的网络应用程序,使我的应用程序离线工作。我如何做到这一点与最新版本的PhoneGap / Cordova(2.0)?

I want to use a pre populated database with my web-app, so that my app works offline. How can i do this with the newest version of PhoneGap / Cordova (2.0)?

我知道此问题之前已被问过,但相对于cordova和iOS的当前版本,所有答案似乎已过期

I know that this question has been asked before, but all answers seem to be out of date relative to the current version of cordova and iOS

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

https://github.com/atkinson/phonegap-prepopulate-db has not been updated for two years https://github.com/davibe/Phonegap-SQLitePlugin has not been updated for 7 months and is for 1.7

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

I found a post here: http://www.raymondcamden.com/index.cfm/2012/7/27/Guest-Blog-Post-Shipping-a-populated-SQLite-DB-with-PhoneGap is this the only way?

使用iOS 6

推荐答案

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

I have used the following plugin for Cordova 2.7 with iOS6 and I see it was just updated a day ago. https://github.com/pgsqlite/PG-SQLitePlugin-iOS

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

They also have an Android version here as well. Part of the problem is PhoneGap changes so often keeping plugins up to date can be time consuming so they lapse. Here is the code for the init method in the AppDelegate.m I use in this project http://www.binpress.com/app/conference-core-ios-for-phonegap/1483

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

Keep in mind that you sometimes need to wipe the iOS Simulator so it reloads the files related to the application.

- (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)



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



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 {

-(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];




}

}

希望这有助于...

这篇关于如何使用PhoneGap / Cordova 2.0预填的SQLite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆