Phonegap离线数据库 [英] Phonegap Offline Database

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

问题描述

我要在加密的数据库中存储用户手机中的一些离线数据(大于100 MB)。如果可能,我还想分发预填充的数据库。我还看到了



我知道 webdatabase 的东西,但因为它贬值,我建议不要使用。



我也看过一些第三方插件,例如 SQLite Plugin ,但它只适用于iOS和Android设备,但我目标4平台(ios,android,黑莓,Windows)



有任何其他解决方案,

解决方案

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



1。 LocalStorage ::

 

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('!!此设备不支持数据库!\\\
\\\
我们很抱歉给您带来的不便,目前正在使用一个版本将在您的手机上工作);
}
db = openDatabase(shortName,version,displayName,maxSize);
createAllTables(db);

创建您的表格

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

执行SQL查询

 交易(function(transaction){
var rowCount = SELECT * FROM Profile';
transaction.executeSql(rowCount,[],function(transaction,result){
if(result.rows.length == 0){
var sqlString =' INSERT INTO配置文件(名称,性别,年龄)VALUES(自己,女性,18)';
transaction.executeSql(sqlString);

}
} ;
});

EDIT ::我忘记添加在最后一个选项:)



3。所有设备上的原生存储



这是Phonegap的最佳组成部分。您可以使用Phonegap插件调用在所有设备上调用本机插件类。在调用期间,您可以将参数传递给类,本机类可以将数据存储在操作系统本身。



例如:在iOS中,您创建一个插件.h& .m类并将其注册到Cordova.plist文件。完成后,您需要使用Phonegap从JavaScript发送对类的调用。一旦使用NSDictionary或任何其他NSArray类型接收到参数,就可以调用CoreData类来存储UNLIMITED数量的数据。



这可以以类似的方式为所有的操作系​​统也完成:)



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



这里是一些关于使用现有SQLite数据库的附加信息。在此示例中,encrypted.db是您创建的全新数据库和pragma。

  ATTACH DATABASE'encrypted.db' '秘密'; - 创建一个新的加密数据库
CREATE TABLE encrypted.t1(a,b); - 重新创建新数据库中的模式(可以使用SELECT * FROM sqlite_master检查所有对象)
INSERT INTO encrypted.t1 SELECT * FROM t1; - 将数据从现有表复制到加密数据库中的新表
DETACH DATABASE encrypted;


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.

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?

解决方案

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

1. LocalStorage ::

Check for localStorage

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

Set an item into LocalStorage

localStorage.setItem("bar", foo);

or

localStorage["bar"] = foo;

Get an item from LocalStorage

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

or

var foo = localStorage["bar"];

2. SQLite Database (more convenient, more persistive)

Set up your DB

var shortName = 'BHCAppDB'; 
var version = '1.0'; 
var displayName = 'BHCAppDB'; 
var maxSize = 65535; 
if (!window.openDatabase){ 
     alert('!! Databases are not supported in this Device !! \n\n 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);

Create your Tables

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)");
}

Execute an SQL Query

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. Native Storage on all devices

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.

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

For Encryption try the following :: SQLCipher

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离线数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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