javascript 使用 sql.js 从磁盘读取 SQLite 数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28066570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Read SQLite database from the disk using sql.js
提问by Div
I am able to read the file when it is being uploaded using - input type="file", but I want to read the SQLite file from a hard coded path. The documentation for sql.js does provide a solution as given below, but i'm not able to get it working. Please help.
我可以在使用 - input type="file" 上传文件时读取文件,但我想从硬编码路径读取 SQLite 文件。sql.js 的文档确实提供了如下所示的解决方案,但我无法使其正常工作。请帮忙。
var fs = require('fs');
var SQL = require('sql.js');
var filebuffer = fs.readFileSync('test.sqlite');
// Load the db
var db = new SQL.Database(filebuffer);
- I hope its very simple and any one using sql.js can answer this.
- 我希望它非常简单,任何使用 sql.js 的人都可以回答这个问题。
回答by agershun
Probably the problem is in the location of sql.js
and test.sqlite
files. If you keep them in the same directory, write the name as './sql.js'
'./test.sqlite'
with dot and slash:
问题可能出在sql.js
和test.sqlite
文件的位置。如果将它们保存在同一目录中,请'./sql.js'
'./test.sqlite'
使用点和斜线写入名称:
var fs = require('fs');
var SQL = require('./sql.js');
var data = fs.readFileSync('./Chinook_Sqlite.sqlite');
var sqldb = new SQL.Database(data);
I just run the code above, and it is works fine with Node.js (node test.js
). My directory has the following files:
我只是运行上面的代码,它在 Node.js ( node test.js
) 上运行良好。我的目录有以下文件:
- test.js - file from above sample
- sql.js - library file
- Chinook_Sqlite.sqlite - Chinook test database
- test.js - 来自上述示例的文件
- sql.js - 库文件
- Chinook_Sqlite.sqlite - Chinook 测试数据库
UPDATE 2People from SQL.js recommend to use this code (see SQL.js wiki) for recent versions of SQL.js:
更新 2来自 SQL.js 的人建议对最新版本的 SQL.js使用此代码(请参阅SQL.js wiki):
var xhr = new XMLHttpRequest();
xhr.open('GET', '/path/to/database.sqlite', true);
xhr.responseType = 'arraybuffer';
xhr.onload = function(e) {
var uInt8Array = new Uint8Array(this.response);
var db = new SQL.Database(uInt8Array);
var contents = db.exec("SELECT * FROM my_table");
// contents is now [{columns:['col1','col2',...], values:[[first row], [second row], ...]}]
};
xhr.send();
UPDATE 1If you need to open SQLite files in the browser, you can use the code below (see the working example here):
更新 1如果您需要在浏览器中打开 SQLite 文件,您可以使用以下代码(请参阅此处的工作示例):
<script src="sql.js"></script>
<script>
function loadBinaryFile(path,success) {
var xhr = new XMLHttpRequest();
xhr.open("GET", path, true);
xhr.responseType = "arraybuffer";
xhr.onload = function() {
var data = new Uint8Array(xhr.response);
var arr = new Array();
for(var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
success(arr.join(""));
};
xhr.send();
};
loadBinaryFile('./Chinook_Sqlite.sqlite', function(data){
var sqldb = new SQL.Database(data);
// Database is ready
var res = db.exec("SELECT * FROM Genre");
});
</script>