Question

Im using HTML5SQL.js in Google Chrome but I only get this error:

Uncaught TypeError: Cannot read property 'sql' of undefined

Please take a look at this script that I'm using: http://jsfiddle.net/mporras/Cx3x5/

Was it helpful?

Solution

Like it says in the guide on htm5sql.com, there are several ways to give html5sql.js sql statements and your error is caused by mixing a couple different ways.

Like xdazz mentioned in his answer, the first argument to html5sql.process can just be a string with one or more sql statements. You can also pass in an array of sql statement strings or sql statement objects. The advantage of using sql statement objects is you can define the data array and specify individual success callbacks.

So essentially you could do this:

$(function() {
   html5sql.openDatabase("demo", "Demo Database", 5 * 1024 * 1024);

   html5sql.process(
       [
            "DROP TABLE IF EXISTS speedtest;",
            "CREATE TABLE speedtest (id INTEGER PRIMARY KEY, label TEXT);",
           {
              sql: "INSERT INTO speedtest VALUES (?, ?);",
              data: [1,'1'],
              success: function(transaction, results) {
                   console.info('Success Inserting First Record');
              }
           },
           {
              sql: "INSERT INTO speedtest VALUES (?, ?);",
              data: [2,'2'],
              success: function(transaction, results) {
                 console.info('Success Inserting Second Record');
              }
           }
       ],
       function() {
          console.log("Final Sucess Callback");
       },
       function(error, failingQuery) {
          console.error("Error: " + error.message);
       }
   );
});

A demo of this can be found in this jsfiddle

OTHER TIPS

The html5sql.process function signature is html5sql.process(SQL, finalSuccessCallback, errorCallback), so try the way below, and here is the demo.

$(function() {
    html5sql.openDatabase("demo", "Demo Database", 5 * 1024 * 1024);

    var st = "DROP TABLE IF EXISTS speedtest; CREATE TABLE speedtest (id INTEGER PRIMARY KEY, label TEXT); INSERT INTO speedtest VALUES (1, '1');INSERT INTO speedtest VALUES (2, '2');";

    html5sql.process( 
      st,
      function(transaction, results) {
         console.info('Success');
      },
      function(error, failingQuery) {
         console.error("Error: " + error.message);
      });
});
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top