javascript - Web sql数据库执行错误

标签 javascript html sql web-sql

我有以下运行良好的 jsFiddle,它显示了 Web sql 功能的正确实现:

https://jsfiddle.net/Trae/76srLbwr/

如果我在 .html 页面上复制完全相同的文件并尝试运行。它没有运行。

HTML:

<html>
<head></head>
<body> 
<h1>WebSQL Example</h1>
<div id="controls">
    <p>Add a car to the database</p>
    <label>Make:</label>
    <input type="text" id="carmake" />
    <br />
    <label>Model:</label>
    <input type="text" id="carmodel" />
    <br />
    <button type="button" id="addcar" onclick="addCar();">Add Car</button>
</div>
<div id="carlistholder">
     <h3>Your Cars</h3>

    <ul id="carlist"></ul>
</div>
<p><strong>Note: </strong>You can leave this page and when you return the cars you entered will still be here!</p>

<script>
//Test for browser compatibility
if (window.openDatabase) {
    //Create the database the parameters are 1. the database name 2.version number 3. a description 4. the size of the database (in bytes) 1024 x 1024 = 1MB
    var mydb = openDatabase("cars_db", "0.1", "A Database of Cars I Like", 1024 * 1024);

    //create the cars table using SQL for the database using a transaction
    mydb.transaction(function (t) {
        t.executeSql("CREATE TABLE IF NOT EXISTS cars (id INTEGER PRIMARY KEY ASC, make TEXT, model TEXT)");
    });

} else {
    alert("WebSQL is not supported by your browser!");
}
//function to output the list of cars in the database
function updateCarList(transaction, results) {
    //initialise the listitems variable
    var listitems = "";
    //get the car list holder ul
    var listholder = document.getElementById("carlist");

    //clear cars list ul
    listholder.innerHTML = "";

    var i;
    //Iterate through the results
    for (i = 0; i < results.rows.length; i++) {
        //Get the current row
        var row = results.rows.item(i);

        listholder.innerHTML += "<li>" + row.make + " - " + row.model + " (<a href='javascript:void(0);' onclick='deleteCar(" + row.id + ");'>Delete Car</a>)";
    }
}

//function to get the list of cars from the database
function outputCars() {
    //check to ensure the mydb object has been created
    if (mydb) {
        //Get all the cars from the database with a select statement, set outputCarList as the callback function for the executeSql command
        mydb.transaction(function (t) {
            t.executeSql("SELECT * FROM cars", [], updateCarList);
        });
    } else {
        alert("db not found, your browser does not support web sql!");
    }
}

//function to add the car to the database
function addCar() {
    //check to ensure the mydb object has been created
    if (mydb) {
        //get the values of the make and model text inputs
        var make = document.getElementById("carmake").value;
        var model = document.getElementById("carmodel").value;

        //Test to ensure that the user has entered both a make and model
        if (make !== "" && model !== "") {
            //Insert the user entered details into the cars table, note the use of the ? placeholder, these will replaced by the data passed in as an array as the second parameter
            mydb.transaction(function (t) {
                t.executeSql("INSERT INTO cars (make, model) VALUES (?, ?)", [make, model]);
                outputCars();
            });
        } else {
            alert("You must enter a make and model!");
        }
    } else {
        alert("db not found, your browser does not support web sql!");
    }
}

//function to remove a car from the database, passed the row id as it's only parameter
function deleteCar(id) {
    //check to ensure the mydb object has been created
    if (mydb) {
        //Get all the cars from the database with a select statement, set outputCarList as the callback function for the executeSql command
        mydb.transaction(function (t) {
            t.executeSql("DELETE FROM cars WHERE id=?", [id], outputCars);
        });
    } else {
        alert("db not found, your browser does not support web sql!");
    }
}

outputCars();
</script>

<style>
body {
    font-family: sans-serif;
    padding: 10px;
}
h1 {
    font-weight: bold;
}
label {
    font-size: small;
}
#controls {
    padding-bottom: 5px;
    border-bottom: 1px solid #000;
}
</style>

</body> 

</html>

错误:

enter image description here

Can someone help me out when I am copying the same files, and opening in same browser Chrome (Version 51.0.2704.84), why is it not running from an html file?

最佳答案

我在chrome中直接浏览html文件时也遇到同样的问题。我可以在 IIS 中创建一个网站来解决它。

希望能帮到你。

丹尼尔.-

关于javascript - Web sql数据库执行错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37852698/

相关文章:

html - 减少 Bootstrap 列表宽度

sql - 新手 SQL 更新问题

javascript - 为什么 indexOf 打印 -1 尽管值在数组中?

javascript - 如何在木瓜中管理缩放和长度 handle

html - 用 CSS 旋转行星

mysql - 我怎样才能加快我的 SQL 查询?

DATE 数据每年的 MYSQL COUNT

javascript - 如何制作标签菜单

javascript - html5 必需的属性来触发 jquery 事件

javascript - 将类应用于前 5 行和后 5 行