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

时间:2012-10-30 11:00:06

标签: ios sqlite web-applications 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

3 个答案:

答案 0 :(得分: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
  

如果__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];
  

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

}

希望这会有所帮助......

答案 1 :(得分:0)

谢谢!没有从这里开始,我就无法做到这一点!

那就是说,这对我来说不起作用,所以我更新了它,并且我对iOS的本机代码并不“好”,所以我非常愿意接受建议。

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

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

让它看起来像这样:

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

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

[fileManager release];

As, according to this answer, my project seemingly automatically releases? I hope?

现在我必须努力将数据库文件放到正确的位置,以便xcode找到它。

希望这有帮助!

答案 2 :(得分:0)

虽然这个问题已经过时了......认为它可能有助于发布我的解决方案 - 2017年发现。(由于PhoneGap已经发生变化,旧的解决方案将无法在Android和Apple应用市场上运行或被接受。)I长期处理这个问题 - 问题是你想要导入一个预先填充的数据库 - 这里没有很多简单的解决方案。我找到了最好也是最简单的 - 目前只是唯一的解决方案是LiteHelpers CORDOVA-SQLITE-EXT(https://github.com/litehelpers/cordova-sqlite-ext

我已将此添加到我的应用中并且运行良好。我不得不从PhoneGap切换 - 并使用CORDOVA - 但它对我有用。

相关问题