开发者

Mobile Safari HTML5 WebSQL SQLTransaction throws SQLError code=1 message='not an error', transaction fails

开发者 https://www.devze.com 2023-02-12 13:59 出处:网络
I\'m trying to populate an HTML5 WebSQL database from a set of SQL statements generated using (sqlite .dump mydb > file.sql)

I'm trying to populate an HTML5 WebSQL database from a set of SQL statements generated using (sqlite .dump mydb > file.sql)

I am able to read in the file, and execute all of the SQL statements. However at the end of the transaction, the transaction fails with a SQLError (code=1, message='not an error').

Now, from my investigations I see SQLite 'not an error' is SQLITE_DONE.

So why is a SQLError being generated, and why is my transaction geting rolled back?

The relavent Javascript fragment is below:

database = {

    /* Transaction Error Callback */
    error_callback: function(error)
    {
        console.log('Oops. '+error.message+' (Code '+error.code+')');
    },

    /* Transaction success callback */
    success_callback: function()
    {
        console.log('apparently a success!');
    },

    populate_exec_sql: function(tx)
    {           
        for (var i开发者_如何学运维 = 0; i < lines.length; i++)   // lines = Global array of SQL statements
        {
            var query = lines[i].replace(/\n/g,'');
            //console.log(query);
            tx.executeSql(query);
        }
    },

    populate_db: function(lines)
    {
        db.transaction( database.populate_exec_sql, database.error_callback, database.success_callback );
    }
}


OK, here's the solution.

Basically the incredibly descriptive and helpful SQLError message, "not an error" is probably being mishandled by webkit / Safari (should probably be ignored).

What happens is if tx.executeSql(query) is being passed a string with no SQL statement in it, it returns a "not an error" SQLError.

In my case this was sometimes an empty string and sometimes a string only containing newline \n.

So, my populate function now looks like this:

populate_exec_sql: function(tx)
{           
    tx.executeSql('drop table if exists "color"');
    tx.executeSql('drop table if exists "combo"');
    tx.executeSql('drop table if exists "combo_color"');
    for (var i = 0; i < lines.length; i++)
    {
        var query = lines[i].replace(/\n/g, '');       // strip newlines
        query && query.length && tx.executeSql(query); // ignore empty lines
    }
}


Have you looked at the specific queries that are failing? SQLite doesn't support all the same query syntax as full SQL, so (for instance) doing an INSERT using UPDATE-like syntax doesn't work.

0

精彩评论

暂无评论...
验证码 换一张
取 消