Skip to content

Load a database from the server

Ophir LOJKINE edited this page Jun 12, 2014 · 3 revisions

Here is how to load a database from your server using XMLHTTPRequest, and open it with sql.js

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.Databse(uInt8Array);
  var contents = db.exec("SELECT * FROM my_table");
  // contents is now [{columns:['col1','col2',...], values:[[first row], [second row], ...]}]
};
xhr.send();