gpt4 book ai didi

javascript - 为什么 Lawnchair 的 webkit-sqlite 适配器将键转换为字符串?

转载 作者:行者123 更新时间:2023-11-29 19:50:48 25 4
gpt4 key购买 nike

我在 webkit-sqlite 适配器上遇到了问题。它以某种方式将 key 保存为字符串格式而不是整数。索引数据库工作得很好。它不会将 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 适配器以添加/编辑行,以便在将键值传递给数据库之前将它们转换为字符串。问题源于 lawnchair 键值所使用的字段(在本例中为 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 适配器将键转换为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17868221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com