mardi 8 décembre 2015

Trouble running SQLite in Javascript (browser, sql.js)

I'm trying to implement a REST environment in which, on the client side, client data is stored in a SQLite database.

I have accomplished this before on Cordova using this plugin, but I'm unable to accomplish the same on it's vanilla js counterpart, sql.js

The problem I have is that the Database() function isn't returning anything, so I can't do any kind of query.

This is my test file (test.html):

<html>
    <head>
        <script src="http://ift.tt/1lM1tsJ" type="text/javascript"></script>
    </head>
    <body>
        <script>
            var sql             = window.SQL;
            var db              = sql.Database(); // Is returning undefined ?
            
            sqlstr = "CREATE TABLE hello (a int, b char);";
            sqlstr += "INSERT INTO hello VALUES (0, 'hello');"
            sqlstr += "INSERT INTO hello VALUES (1, 'world');"
            db.run(sqlstr); // Run the query without returning anything

            var res = db.exec("SELECT * FROM hello");

            console.log(res);
        </script>
    </body>
</html>

This returns the following error:

TypeError: this.handleError is not a function sql.js Line 466

If I create this.handleError() by myself, the error changes to:

TypeError: db is undefined test.html Line 13

Any ideas on how to solve this problem? I have looked all over the place, but documentation seems to be scarce.

Thanks

Aucun commentaire:

Enregistrer un commentaire