JSONStore difference between 'number' and 'integer' in searchFields

不想你离开。 提交于 2019-12-02 07:14:30

问题


I have a question about JSONStore searchFields.

If I use number as the searchFields key and try to find data by WL.JSONStore.find method with 0 as the query, It will hit all data (not filtered).

With the integer of the case above works fine.

What's the difference between number and integer?


回答1:


JSONStore uses SQLite to persist data, you can read about SQLite Data Types here. The short answer is number will store data as REAL while integer will store data as INTEGER.

If you create a collection called nums with one searchField called num of type number

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

and add some data:

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

then call find with the query: {num: 0}

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

you should get back:

[{"_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}}]

Notice that you got back all the documents you stored (num = 4, 3, 2, 1, 0).

If you look at the .sqlite file:

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

(The android file should be under /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);

Notice the data type for num is REAL.

Running a query the same query used in the find function:

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

Notice 4 is stored as 4.0 and JSONStore's queries always use LIKE, any num with a 0 will match the query.

If you use integer instead:

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

Find returns:

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

The schema shows that num has an INTEGER data type:

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

I skipped some of the onSuccess and all the onFailure callbacks for brevity.




回答2:


The actual difference between a JSON number and integer is

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

Hope you understand



来源:https://stackoverflow.com/questions/15423015/jsonstore-difference-between-number-and-integer-in-searchfields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!