AlaSQL / alasql

AlaSQL.js - JavaScript SQL database for browser and Node.js. Handles both traditional relational tables and nested JSON data (NoSQL). Export, store, and import data from localStorage, IndexedDB, or Excel.
http://alasql.org
MIT License
7.02k stars 656 forks source link

can not select use where in json file #1841

Open noday opened 10 months ago

noday commented 10 months ago

vue3 ts ··· // it is work, and console print [{...}] let arr = [{ "name": "中文", "addr": "中文" }, { "name": "文", "addr": "文" }]; let res = alasql("SELECT * FROM ? WHERE name like '%文%'", [arr]);

// but use json file, not work, console print [] alasql.promise("SELECT * FROM json('/test.json') WHERE name like '%文%'").then(function (results) { console.log(results); }).catch(console.error); ··· test.json image

both file encode utf8

mathiasrw commented 10 months ago

Are you getting anything from console.error?

Is /test.json the correct path its absolute - are you sure you did not mean ./test.json? Can you load the file manually from the browser?

noday commented 10 months ago

error

no error, if use letter in json file, it can search. WHERE name like '%q%' image

noday commented 10 months ago

the '中文' encode to x96x87, maybe encode type used incorrect

noday commented 10 months ago

image it is json file

mathiasrw commented 10 months ago

Interesting. Any chance you can share the actual json file so we can try a bit?

noday commented 10 months ago

test.json version "alasql": "^4.2.2", code

import alasql from "alasql";

alasql.promise("SELECT * FROM json('/test.json') WHERE name like '%q%'").then(function (results) {
    console.log(results);
}).catch(console.error);

// let arr = [{ "name": "中文", "addr": "中文" }, { "name": "文", "addr": "文" }];
// let res = alasql("SELECT * FROM ? WHERE name like '%文%'", [arr]);
// console.log(res);
bgb10 commented 9 months ago

I wanted to solve this issue, so I tried to reproduce the error. But, it seems to work well in the same situation. Below is the single test.js file for reproducing bug, and I included test.json in the same directory. Only included alasql dependency.

const alasql = require('alasql')

let arr = [
  { name: '中文', addr: '中文' },
  { name: '文', addr: '文' }
]

let res = alasql("SELECT * FROM ? WHERE name like '%文%'", [arr])
console.log(res)

alasql
  .promise("SELECT * FROM json('./test.json') WHERE name like '%文%'")
  .then(function (results) {
    console.log(results)
  })
  .catch(console.error)

I think there's no encoding problem or error occured. I only encountered an error if I write the file location in absolute path way (like /test.json). I think that could be the problem. If I'm missing an error, please let me know. Thank you! ☺️

Screenshot 2023-12-28 at 12 29 19 AM Screenshot 2023-12-28 at 12 29 34 AM