Security_ERR:DOM Exception 18 openDatabase()使用Phonegap

时间:2012-10-25 13:46:51

标签: cordova

我使用phonegap模板创建了HTML5 / JQuery项目。我试图在本地数据库上存储信息。目前,我正在使用模拟器ElectricMobileStudio2012进行测试。 我在这行代码中收到错误“ Security_ERR:DOM Exception 18 ”:

var db = window.openDatabase("MobileInspection", "1.0", "Mobile Inspection Database", 200000);

这是我的代码。我已经坚持了几天,我甚至尝试使用这些代码来解决错误:

navigator.openDatabase = window.openDatabase = DroidDB_openDatabase;

window.droiddb = new DroidDB();

这是我的代码:

function SaveUserInfoLocally(data) {
    try {
        var rememberMe = $('#chkRememberMe').is(':checked')
        if (rememberMe) {
            // TODO: Save user details in local db.
            //navigator.openDatabase = window.openDatabase = DroidDB_openDatabase;
            //window.droiddb = new DroidDB();
            var db = window.openDatabase("MobileInspection", "1.0", "Mobile Inspection Database", 200000);
            db.transaction(populateUsersTable, errorCB, successCB);
            db.transaction(queryDB, errorCB);
        }
    }
    catch (error) {
        alert(error);
    }
}

function populateUsersTable(tx) {
    try {
        var userName = window.localStorage.getItem("UserName");
        var firstName = window.localStorage.getItem("FirstName");
        var lastName = window.localStorage.getItem("LastName");
        //alert(userName + " " + firstName + " " + lastName);
        tx.executeSql("DROP TABLE IF EXISTS UserDetails");
        tx.executeSql("CREATE TABLE IF NOT EXISTS UserDetails (UserName, FirstName, LastName)");
        tx.executeSql("INSERT INTO UserDetails (UserName, FirstName, LastName) VALUES ('" + userName + "', '" + firstName + "', '" + lastName + "')");
        alert("populate");
    }
    catch (exception) {
        alert(exception);
    }
}

function errorCB(err) {
    alert("Error processing: " + err);
}

function successCB() {
    alert("success!"); 
}

function queryDB(tx) {
    try {
        tx.executeSql('SELECT * FROM UserDetails', [], querySuccess, errorCB);
    }
    catch (exception) {
        alert(exception);
    }
}

// Testing
function querySuccess(tx, results) {
    try {
        if (results) {
            alert("records");
        }
        else {
            alert(results);
        }
    }
    catch (exception) {
        alert(exception);
    }
}

1 个答案:

答案 0 :(得分:3)

我有类似的问题 - 因此我在这个问题上遇到了绊脚石。

确保所有localstorage调用都在deviceready块中执行:

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady() {
    //do ALL your localstorage stuff here
}