Search code examples
javascriptalasql

How to have persistant database using alasql


I've try this on Codepen:

var result = db.exec('CREATE DATABASE IF NOT EXISTS MyBase; \
                      ATTACH INDEXEDDB DATABASE MyBase;\
                      USE MyBase;'+command)[3];

but after first command I've got exception that database exists, if I don't use create database I've got error that database don't exists

if I've call CREATE DATABASE IF NOT EXISTS MyBase ones at the beginning then I've got error that table don't exist even that I've called create table.

If I've use:

CREATE INDEXEDDB DATABASE IF NOT EXISTS MyBase

I've got error that database don't exists.

I've also tried to execute:

window.indexedDB.open("MyBase", 3);

but that also don't work.

And if I don't use indexDB the tables are cleared after refresh.

Here is my codepen https://codepen.io/jcubic/pen/dVBaRm?editors=0010


Solution

  • Index db require async code:

    alasql('CREATE DATABASE IF NOT EXISTS MyBase; \
            ATTACH INDEXEDDB DATABASE MyBase;\
            USE MyBase;', [], function() {
        alasql(command, [], function(res) {
        });
    });
    

    it's on the wiki https://github.com/agershun/alasql/wiki/IndexedD