Javascript 移动HTML5应用程序本地存储
快速概述我所拥有的以及与之相关的问题。我有一个HTML5应用程序,我将使用PhoneGap作为本机应用程序运行。这是一个小型的数据采集应用程序,用于本地现场研究站 该现场站的问题是,它们容易丢失传输数据的信号;因此,我希望将数据存储在设备上以供以后使用(或将其发送到服务器) 需要保存的数据如下所示:Javascript 移动HTML5应用程序本地存储,javascript,html,local-storage,web-sql,indexeddb,Javascript,Html,Local Storage,Web Sql,Indexeddb,快速概述我所拥有的以及与之相关的问题。我有一个HTML5应用程序,我将使用PhoneGap作为本机应用程序运行。这是一个小型的数据采集应用程序,用于本地现场研究站 该现场站的问题是,它们容易丢失传输数据的信号;因此,我希望将数据存储在设备上以供以后使用(或将其发送到服务器) 需要保存的数据如下所示: *User Name (string) *Transect Color (string) *Transect Number (int) *Transect Board (char) *Salaman
*User Name (string)
*Transect Color (string)
*Transect Number (int)
*Transect Board (char)
*Salamander Name (string)
*Snount-Vent Length (int)
*Total Length (int)
*Centipedes (int)
*Red-Black Millipedes (int)
*Brown Round Millipedes (int)
*Brown Flat Millipedes (int)
var temp = localStorage.getItem('mydata');
if(temp != null) {
var dataJSON = JSON.parse(temp);
}
localStorage.clearItem('mydata');
var x = []; // take an array to store all data
x.push({a: 'x', b: 'y'});
localStorage.setItem('mydata', JSON.stringify(x));
// for further entry
var temp = JSON.parse(localStorage.getItem('mydata')); // retrieve localStroage item
temp.push({a: 'xx', b: 'yy'}) // push new data to array
localStorage.setItem('mydata', JSON.stringify(t)); // overwrite the existing entry with new data
console.log(JSON.parse(localStorage.getItem('data'))); // you will see your all data here
该应用程序本身将是一个独立的应用程序,所以我希望它有一种方式来保存数据
使用jQuery和HTML5的组合,最好的方法是什么?我知道Web SQL已被弃用,但我相信这可能是我存储这些数据的唯一方法。如果不是,在发送之前动态存储X个条目的最佳方式是什么
谢谢你的帮助
编辑:
当研究人员必须进行“全断面”时,这种情况如何
研究人员必须从一个地点到另一个地点,直到完成每个样带的20个完整数据收集。这需要20个不同的地点,每个地点都有不同数量的蝾螈和动物进入
当localstorage
仅允许键:值对时,如何存储要在最后卸载的更大的数据集?例如:
var data = {
name: 'x',
transect_color: 'red'.
.....
..
};
localStorage.setItem('mydata', JSON.stringify(data)); // mydata is the name of storage
要获取数据,您可以尝试以下操作:
*User Name (string)
*Transect Color (string)
*Transect Number (int)
*Transect Board (char)
*Salamander Name (string)
*Snount-Vent Length (int)
*Total Length (int)
*Centipedes (int)
*Red-Black Millipedes (int)
*Brown Round Millipedes (int)
*Brown Flat Millipedes (int)
var temp = localStorage.getItem('mydata');
if(temp != null) {
var dataJSON = JSON.parse(temp);
}
localStorage.clearItem('mydata');
var x = []; // take an array to store all data
x.push({a: 'x', b: 'y'});
localStorage.setItem('mydata', JSON.stringify(x));
// for further entry
var temp = JSON.parse(localStorage.getItem('mydata')); // retrieve localStroage item
temp.push({a: 'xx', b: 'yy'}) // push new data to array
localStorage.setItem('mydata', JSON.stringify(t)); // overwrite the existing entry with new data
console.log(JSON.parse(localStorage.getItem('data'))); // you will see your all data here
如果要清除本地存储数据,请执行以下操作:
*User Name (string)
*Transect Color (string)
*Transect Number (int)
*Transect Board (char)
*Salamander Name (string)
*Snount-Vent Length (int)
*Total Length (int)
*Centipedes (int)
*Red-Black Millipedes (int)
*Brown Round Millipedes (int)
*Brown Flat Millipedes (int)
var temp = localStorage.getItem('mydata');
if(temp != null) {
var dataJSON = JSON.parse(temp);
}
localStorage.clearItem('mydata');
var x = []; // take an array to store all data
x.push({a: 'x', b: 'y'});
localStorage.setItem('mydata', JSON.stringify(x));
// for further entry
var temp = JSON.parse(localStorage.getItem('mydata')); // retrieve localStroage item
temp.push({a: 'xx', b: 'yy'}) // push new data to array
localStorage.setItem('mydata', JSON.stringify(t)); // overwrite the existing entry with new data
console.log(JSON.parse(localStorage.getItem('data'))); // you will see your all data here
您可以存储多个数据,如下所示:
*User Name (string)
*Transect Color (string)
*Transect Number (int)
*Transect Board (char)
*Salamander Name (string)
*Snount-Vent Length (int)
*Total Length (int)
*Centipedes (int)
*Red-Black Millipedes (int)
*Brown Round Millipedes (int)
*Brown Flat Millipedes (int)
var temp = localStorage.getItem('mydata');
if(temp != null) {
var dataJSON = JSON.parse(temp);
}
localStorage.clearItem('mydata');
var x = []; // take an array to store all data
x.push({a: 'x', b: 'y'});
localStorage.setItem('mydata', JSON.stringify(x));
// for further entry
var temp = JSON.parse(localStorage.getItem('mydata')); // retrieve localStroage item
temp.push({a: 'xx', b: 'yy'}) // push new data to array
localStorage.setItem('mydata', JSON.stringify(t)); // overwrite the existing entry with new data
console.log(JSON.parse(localStorage.getItem('data'))); // you will see your all data here
例如:
var data = {
name: 'x',
transect_color: 'red'.
.....
..
};
localStorage.setItem('mydata', JSON.stringify(data)); // mydata is the name of storage
要获取数据,您可以尝试以下操作:
*User Name (string)
*Transect Color (string)
*Transect Number (int)
*Transect Board (char)
*Salamander Name (string)
*Snount-Vent Length (int)
*Total Length (int)
*Centipedes (int)
*Red-Black Millipedes (int)
*Brown Round Millipedes (int)
*Brown Flat Millipedes (int)
var temp = localStorage.getItem('mydata');
if(temp != null) {
var dataJSON = JSON.parse(temp);
}
localStorage.clearItem('mydata');
var x = []; // take an array to store all data
x.push({a: 'x', b: 'y'});
localStorage.setItem('mydata', JSON.stringify(x));
// for further entry
var temp = JSON.parse(localStorage.getItem('mydata')); // retrieve localStroage item
temp.push({a: 'xx', b: 'yy'}) // push new data to array
localStorage.setItem('mydata', JSON.stringify(t)); // overwrite the existing entry with new data
console.log(JSON.parse(localStorage.getItem('data'))); // you will see your all data here
如果要清除本地存储数据,请执行以下操作:
*User Name (string)
*Transect Color (string)
*Transect Number (int)
*Transect Board (char)
*Salamander Name (string)
*Snount-Vent Length (int)
*Total Length (int)
*Centipedes (int)
*Red-Black Millipedes (int)
*Brown Round Millipedes (int)
*Brown Flat Millipedes (int)
var temp = localStorage.getItem('mydata');
if(temp != null) {
var dataJSON = JSON.parse(temp);
}
localStorage.clearItem('mydata');
var x = []; // take an array to store all data
x.push({a: 'x', b: 'y'});
localStorage.setItem('mydata', JSON.stringify(x));
// for further entry
var temp = JSON.parse(localStorage.getItem('mydata')); // retrieve localStroage item
temp.push({a: 'xx', b: 'yy'}) // push new data to array
localStorage.setItem('mydata', JSON.stringify(t)); // overwrite the existing entry with new data
console.log(JSON.parse(localStorage.getItem('data'))); // you will see your all data here
您可以存储多个数据,如下所示:
*User Name (string)
*Transect Color (string)
*Transect Number (int)
*Transect Board (char)
*Salamander Name (string)
*Snount-Vent Length (int)
*Total Length (int)
*Centipedes (int)
*Red-Black Millipedes (int)
*Brown Round Millipedes (int)
*Brown Flat Millipedes (int)
var temp = localStorage.getItem('mydata');
if(temp != null) {
var dataJSON = JSON.parse(temp);
}
localStorage.clearItem('mydata');
var x = []; // take an array to store all data
x.push({a: 'x', b: 'y'});
localStorage.setItem('mydata', JSON.stringify(x));
// for further entry
var temp = JSON.parse(localStorage.getItem('mydata')); // retrieve localStroage item
temp.push({a: 'xx', b: 'yy'}) // push new data to array
localStorage.setItem('mydata', JSON.stringify(t)); // overwrite the existing entry with new data
console.log(JSON.parse(localStorage.getItem('data'))); // you will see your all data here
我是迷你键值商店的超级粉丝:
window.storage = {
store:localStorage,
get: function( key ) {
try {
return JSON.parse(this.store[key]);
} catch(e) {};
return undefined;
},
set: function( key, value) {
try {
this.store[key] = JSON.stringify(value);
} catch(e) {};
}
}
这使您能够直接引用window.storage
。假设已将所述字段包装在名为行
的散列中,现在可以直接序列化:
storage.set('myrow', row);
由于存储在行
中的数据将被序列化,因此可以创建一个文档
,该文档包含的信息比单个SQL行可能包含的信息多得多:
var document = {
property: 'foo',
nestedArray: [ 'foo', 'bar', 'baz' ],
nestedObj: { 'key': 'foo', 'key2': 'foo' }
}
storage.set('transectID', document);
…你明白了。关键是,即使是非常丰富的数据也可以通过JSON.stringify减少,并以可恢复的形式保存。我非常喜欢使用迷你键值存储:
window.storage = {
store:localStorage,
get: function( key ) {
try {
return JSON.parse(this.store[key]);
} catch(e) {};
return undefined;
},
set: function( key, value) {
try {
this.store[key] = JSON.stringify(value);
} catch(e) {};
}
}
这使您能够直接引用window.storage
。假设已将所述字段包装在名为行
的散列中,现在可以直接序列化:
storage.set('myrow', row);
由于存储在行
中的数据将被序列化,因此可以创建一个文档
,该文档包含的信息比单个SQL行可能包含的信息多得多:
var document = {
property: 'foo',
nestedArray: [ 'foo', 'bar', 'baz' ],
nestedObj: { 'key': 'foo', 'key2': 'foo' }
}
storage.set('transectID', document);
…你明白了。关键是,即使是非常丰富的数据也可以通过JSON.stringify减少,并以可恢复的形式保存。将大型数据集存储在
localStorage
中不是一个好主意
不推荐使用WebSQL。是浏览器中较大数据集的首选工具,但PhoneGap中尚未提供(尽管它应该在路线图上)
与直觉相反,尽管WebSQL规范不会进一步开发,但IndexedDB仍然是一个非常新的选项
如果您使用localStore,就像使用mobile一样,您应该尽可能信任内存中的表示,并且只有在无法从内存中读取数据时才从“磁盘”中读取。在
本地存储中存储大型数据集不是一个好主意
不推荐使用WebSQL。是浏览器中较大数据集的首选工具,但PhoneGap中尚未提供(尽管它应该在路线图上)
与直觉相反,尽管WebSQL规范不会进一步开发,但IndexedDB仍然是一个非常新的选项
如果您使用localStore,就像使用mobile一样,您应该尽可能信任内存中的表示,并且只有在无法从内存中读取数据时才从“磁盘”中读取。您似乎并不真正需要数据存储的SQL部分来存储一些数据localStorage
是活跃的,即使在IE中也是如此。我也会选择localStorage
,但FWIW在包括Chrome在内的许多实现中都是“非常慢”的,您似乎并不真的需要数据存储的SQL部分来存储一些数据localStorage
即使在IE中也能正常运行。我也会选择localStorage
,但FWIW在包括Chrome在内的许多实现中都“非常慢”。我更新了我的解释,更广泛地讨论了基于文档的存储的可能性(这听起来可能是一个更适合您的解决方案?),编辑答案并将其更改为其他内容。它不是只读的,作为作用域局部变量(例如,在函数中)。你不应该编辑别人的答案。你可以评论或提供更好的答案。也许可以联系作者,让他改进他的答案。是的。我更新了我的解释,更广泛地讨论了基于文档的存储的可能性(这听起来可能是一个更适合您的解决方案?),编辑答案并将其更改为其他内容。它不是只读的,作为作用域局部变量(例如,在函数中)。你不应该编辑别人的答案。你可以评论或提供更好的答案。也许可以联系作者,让他改进他的答案。