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);
}
});
});
编辑 :: 我忘了添加最后一个选项