-1

In my effort to learn more about WebSQL, I've ran across an "Errorundifined" in my javascript code. I've tested the code and can't seem to find the error. However, The database "To Do" gets created but not the table "Tasks". Can someone help!

<!doctype html>
<html lang="en">
    <head>
    <meta charset="utf-8">
    <title>Untitled Document</title>
    <script type="text/javascript" src="../js/jquery.js"></script>
    <script>
        var db = null;
        var db = openDatabase('To Do', '1.0', 'To Do', 2 * 1024 * 1024);
        db.transaction(function(tx) {
            tx.executeSql('CREATE TABLE IF NOT EXISTS tasks (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, create unique, name, description, due DATETIME)');
});

        $('document').ready(function() {
            var db = openDatabase('To Do', '1.0', 'To Do', 2 * 1024 * 1024);        
            db.transaction(function(tx) {
                tx.executeSql('SELECT * FROM tasks', [], querySuccess, errorCB);
    });

        function querySuccess(tx, results) {
            var len = results.rows.length;
                if(len > 0) {   

             }else{
                alert("You Have No Tasks");

                }
        }

        function errorCB(err) {
            alert("Error" + err.code);  
        }

    });

    </script>
    </head>

    <body>

        <h1>Hello World!</h1>

    </body>
</html>
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • please provide us with code. – Kevin Kloet Nov 24 '16 at 12:13
  • I'm trying to build an html5 relational database for an offline website for internal use only, without using a backend server. This is all of the code that I have so far. I can't get past the "Errorundefind" error in the javascript. – Mister Tech Nov 24 '16 at 12:42

1 Answers1

0

The main problem you're running in to debugging your program is that the signature of your error callback is incorrect, it passes two parameters, the transaction and then the error (similar to how the success callback passes the transaction and the results).

if you change your errorCB to

function errorCB(tx,err) {
    console.log(err);
}

You'll see the error is:

SQLError {code: 5, message: "could not prepare statement (1 no such table: tasks)"}

Live example: https://jsfiddle.net/toscqrzz/

And if you provide the same callback to the original CREATE TABLE you'll see

SQLError {code: 5, message: "could not prepare statement (1 near "create": syntax error)"}

Live example: https://jsfiddle.net/ykhL9va6/

Jamiec
  • 133,658
  • 13
  • 134
  • 193