javascriptweb-sql

Check if index exists in WebSQL


Is there a way to check if an index exists for a table in WebSQL database? I'm using CREATE UNIQUE INDEX INDEX_GUID ON TABLE PERSONS for creation. But if index already exists I get an error:

{
    code: 5, // SYNTAX_ERR - from docs
    message: "could not prepare statement (1 index INDEX_GUID already exists)"
}

The best solution for me would be to list all indexes of a table.


Solution

  • I am using this but it's a workaround. I hope that error messages are allways in english.

    db.transaction(function (tx) {
        tx.executeSql(
            "CREATE UNIQUE INDEX INDEX_GUID ON TABLE PERSONS",
            [],
            function (tx, rs) {
                // index created
            },
            function (tx, e) {
                if (e.message.indexOf("already exists") == -1) {
                    // index not created - already exists
                }
            });
        );
    });