我在 webkit-sqlite 适配器上遇到问题。它以某种方式将key
保存为字符串格式而不是整数。 Indexed-db 工作得很好。它不会将 key 转换为字符串。请参阅下面的代码。
var ppl = Lawnchair({adapter: 'webkit-sqlite', name:'people', record:'person'}, function(people) {
// anon fn bound to the instance
this.save({key:1, id:1, a:1, name:'nino'}, function(obj){
console.log(obj);
});
// anon fn bound to the instance
this.save({key:'2', id:2, a:2, name:'paolo'}, function(obj){
console.log(obj);
});
// get all the keys
this.keys(function(keys) {
console.log('keys:', keys);
});
// get 1
this.get(1, function(key) {
console.log('key:', key);
});
// get '2'
this.get('2', function(key) {
console.log('key:', key);
});
// we can also clear the entire collection w/ nuke
this.nuke()
});
输出:
undefined
Object {key: 1, id: 1, a: 1, name: "nino"}
Object {key: "2", id: 2, a: 2, name: "paolo"}
keys: ["1.0", "2"]
key: undefined
key: Object {key: "2", id: 2, a: 2, name: "paolo"}
错误:
参见 keys: ["1.0", "2"]
它应该是 keys: [1, "2"]
有人有这个补丁吗?
谢谢。
最佳答案
具有讽刺意味的是,我今天在尝试草坪椅时遇到了完全相同的问题。我最终决定修改 webkit-sqlite 适配器以添加/编辑行,以便在将键值传递到数据库之前将它们转换为字符串。该问题源于以下事实:草坪椅键值所使用的字段(在本例中为 id
)必须建立为 NVARCHAR(32) 数据类型,出于兼容性原因,这是有意义的。请记住,您可以将 webkit-sqlite 数据库与基于整数的 id 字段一起使用,但尝试将其与 lawnchair 集成会很复杂或不可能。我的解决方案是在将数值传递到数据库之前将其转换为字符串。这可以在不修改适配器的情况下完成,但出于我的目的,我想在 indexeddb 可用时将整数用作整数。
这是我的代码的链接:http://wemarketyour.com/lawnchair-using-indexeddb-websql-dom-localstorage-adapters/
我在 webkit-sqlite 适配器中修改代码的行已突出显示。与我的版本的不同之处在于,虽然仍然转换为字符串,但数据被保留。因此,您得到的不是 int 1 => string "1.0",而是 int 1 => string "1"。稍后当您检索数据时,您可以使用 parseInt(key) 将 key 转换回整数。
关于javascript - 为什么 Lawnchair 的 webkit-sqlite 适配器将 key 转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17868221/