Phonegap 离线数据库

Phonegap Offline Database(Phonegap 离线数据库)

本文介绍了Phonegap 离线数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将用户手机中的一些离线数据(超过 100 MB)存储在加密数据库中.如果可能,我还想分发预先填充的数据库.我也看过这个.

I want to store some large offline data in user phone (more than 100 MB) in an encrypted database. If possible I also want to distribute the database pre-populated. I have also seen this.

我知道网络数据库的事情,但因为它是折旧,建议我不要使用它.

I know about the webdatabase thing, but because it is depreciated, I am advised not to work with that.

我也见过一些第三方插件,例如 SQLite Plugin,但它只适用于 iOS 和 Android 设备,但我的目标是 4 个平台(ios、android、blackberry、windows)

I also have seen some third party plugins such as SQLite Plugin, but it works only for iOS and Android devices, but I target 4 platforms (ios, android, blackberry, windows)

除了写下我自己的解决方案,还有其他解决方案吗?

Is there any other solution, other than writing down my own?

推荐答案

我最近制作了一个需要这个的应用程序,针对相同的操作系统.您可以使用 2 个数据库的组合:

I made an app recently that required this, targetting the same OS's. You can use a combination of 2 databases :

1.本地存储::

检查本地存储

function supports_html5_storage() {
  try {
    return 'localStorage' in window && window['localStorage'] !== null;
  } catch (e) {
    return false;
  }
}

将项目设置到 LocalStorage

localStorage.setItem("bar", foo);

localStorage["bar"] = foo;

从 LocalStorage 获取一个项目

var foo = localStorage.getItem("bar");

var foo = localStorage["bar"];

2.SQLite 数据库(更方便,更持久)

设置您的数据库

var shortName = 'BHCAppDB'; 
var version = '1.0'; 
var displayName = 'BHCAppDB'; 
var maxSize = 65535; 
if (!window.openDatabase){ 
     alert('!! Databases are not supported in this Device !! 

 We are sorry for the inconvenience and are currently working on a version that will work on your phone'); 
}
db = openDatabase(shortName, version, displayName,maxSize);
createAllTables(db);

创建表格

function createAllTables(db){
    db.transaction(function(transaction){
        transaction.executeSql("CREATE TABLE IF NOT EXISTS Profile(id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT, gender TEXT,age INTEGER)");
}

执行 SQL 查询

transaction(function(transaction){
        var rowCount = 'SELECT * FROM Profile';
        transaction.executeSql(rowCount,[],function(transaction,result){
            if(result.rows.length == 0){
                var sqlString = 'INSERT INTO Profile (name,gender,age) VALUES("自己","Female",18)';
                transaction.executeSql(sqlString);

            }
        });
    });

编辑 :: 我忘了添加最后一个选项 :)

EDIT :: I forgot to add in the last option :)

3.所有设备上的本机存储

这是 Phonegap 最好的部分.您可以使用 Phonegap 插件调用在所有设备上调用本机插件类.在调用过程中,您可以将参数传递给类,本机类可以将您的数据存储在操作系统本身中.

This is the best part of Phonegap. You can call a native plugin class on all the devices using the Phonegap plugin call. During the call, you can pass parameters to the class, and the native class can store your data in the OS itself.

例如 :: 在 iOS 中,您创建了一个插件 .h &.m 类并将其注册到 Cordova.plist 文件中.完成后,您需要使用 Phonegap 从 JavaScript 向该类发送调用.一旦使用 NSDictionary 或任何其他 NSArray 类型接收到参数,您就可以调用 CoreData 类来存储无限量的数据.你永远不会耗尽内存.

For example :: in iOS, you create a plugin .h & .m class and register it with the Cordova.plist file. Once that's done, you need to send a call to the class from JavaScript using Phonegap. Once the parameters have been received using NSDictionary or any other NSArray type, you can call a CoreData class to store UNLIMITED amounts of data. You'll never run out of memory .

对于所有其余的操作系统也可以以类似的方式完成:)

This can be done in a similar fashion for all the rest of the OS's also :)

对于加密,请尝试以下 :: SQLCipher

For Encryption try the following :: SQLCipher

以下是有关使用现有 SQLite 数据库的一些附加信息.在此示例中, encrypted.db 是您创建的全新数据库和编译指示.

Here is some additional information on working with an existing SQLite database. In this example encrypted.db is that brand new database you create and pragma.

ATTACH DATABASE 'encrypted.db' AS encrypted KEY 'secret'; -- create a new encrypted database
CREATE TABLE encrypted.t1(a,b); -- recreate the schema in the new database (you can inspect all objects using SELECT * FROM sqlite_master)
INSERT INTO encrypted.t1 SELECT * FROM t1; -- copy data from the existing tables to the new tables in the encrypted database
DETACH DATABASE encrypted;

这篇关于Phonegap 离线数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Phonegap 离线数据库