检查WebSQL中是否存在索引

问题描述 投票:0回答:2

有没有办法检查 WebSQL 数据库中的表是否存在索引? 我正在使用

CREATE UNIQUE INDEX INDEX_GUID ON TABLE PERSONS
进行创作。但是如果索引已经存在,我会得到一个错误:

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

对我来说最好的解决方案是列出表的所有索引。

javascript web-sql
2个回答
0
投票

我正在使用它,但这是一种解决方法。我希望错误信息总是英文的。

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
            }
        });
    );
});

0
投票

if (e.message.indexOf("already exists") == -1) {

应该是一个!=

    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
                }
            });
        );
     });
© www.soinside.com 2019 - 2024. All rights reserved.