-->

在searchFields“编号”和“整数”之间JSONStore差(JSONStore diffe

2019-08-17 14:46发布

我有一个关于JSONStore searchFields问题。

如果我使用number由作为searchFields键,并设法找到数据WL.JSONStore.find0作为查询,这将打击所有的数据(未过滤)。

随着integer的情况下上述工作正常。

什么之间的差异numberinteger

Answer 1:

JSONStore使用SQLite留存数据,你可以看到SQLite的数据类型在这里 。 简短的回答是number将数据存储为REAL ,而integer将数据存储为INTEGER

如果您创建一个名为集合nums一个searchField名为num的类型number

var nums = WL.JSONStore.initCollection('nums', {num: 'number'}, {});

并添加一些数据:

var len = 5;
while (len--) {
    nums.add({num: len});
}

然后调用find与查询: {num: 0}

nums.find({num: 0}, {onSuccess: function (res) {
    console.log(JSON.stringify(res));
}})

你应该得到的结果:

[{"_id":1,"json":{"num":4}},{"_id":2,"json":{"num":3}},{"_id":3,"json":{"num":2}},{"_id":4,"json":{"num":1}},{"_id":5,"json":{"num":0}}]

请注意,你回来你存储的文件(NUM = 4,3,2,1,0)。

如果你看一下.sqlite文件:

$ cd ~/Library/Application Support/iPhone Simulator/6.1/Applications/[id]/Documents
$ sqlite3 jsonstore.sqlite

(Android的文件应该是下/data/data/com.[app-name]/databases/

sqlite> .schema
CREATE TABLE nums ( _id INTEGER primary key autoincrement,  'num' REAL, json BLOB, _dirty REAL default 0, _deleted INTEGER default 0, _operation TEXT);

请注意为NUM的数据类型是REAL

运行查询,在查找功能中使用相同的查询:

sqlite> SELECT * FROM nums WHERE num LIKE '%0%';
1|4.0|{"num":4}|1363326259.80431|0|add
2|3.0|{"num":3}|1363326259.80748|0|add
3|2.0|{"num":2}|1363326259.81|0|add
4|1.0|{"num":1}|1363326259.81289|0|add
5|0.0|{"num":0}|1363326259.81519|0|add

通知4存储为4.0和JSONStore的请求总是使用LIKE ,与任何NUM 0将匹配查询。

如果使用integer ,而不是:

var nums = WL.JSONStore.initCollection('nums', {num: 'integer'}, {});

查找回报:

[{"_id":5,"json":{"num":0}}]

schema显示,NUM具有INTEGER数据类型:

sqlite> .schema
CREATE TABLE nums ( _id INTEGER primary key autoincrement,  'num' INTEGER, json BLOB, _dirty REAL default 0, _deleted INTEGER default 0, _operation TEXT);

sqlite> SELECT * FROM nums WHERE num LIKE '%0%';
5|0|{"num":0}|1363326923.44466|0|add

我跳过一些的onSuccess和所有的onFailure为简洁回调。



Answer 2:

一个JSON数和整数之间的实际差别是

defining {age: 'number'} indexes 1 as 1.0,
while defining{age: 'integer'} indexes 1 as 1.

希望你能理解



文章来源: JSONStore difference between 'number' and 'integer' in searchFields