vendredi 25 décembre 2015

How do I load an SQLite database from file structure in a browser?

I recently started learning JavaScript by making a Pokemon battle simulator using the tutorial from here but I wanted to make it a step further by adding a database. I found one premade on GitHub. My question is how do I load the database based on this file structure? I've tried using sql.js but to no success.

In my HTML file, I added the line <script data-main="js/main" src="js/require.js"></script> in order to get the require method to work for requirejs

Inside main.js, I have:

    var sqliteDatabase;

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 = [];
        for (var i = 0; i != data.length; ++i) arr[i] = String.fromCharCode(data[i]);
        success(arr.join(""));
    };
    xhr.send();
}

require(['js/sql.js'], function (sql) {
    loadBinaryFile('db/pokedex.sqlite', function (data) {
        sqliteDatabase = new sql.Database(data);
    })
});

Aucun commentaire:

Enregistrer un commentaire