javascript - SQL 插入语句在 Javascript 中不起作用

标签 javascript sql json html web-sql

我正在尝试使用 JSON 数组填充我的 WebSQL 数据库(对象称为 myJSONObject,数组称为 costcodes)。

点击运行函数,数据库创建成功,但数据没有插入数据库。它甚至不会抛出错误,它只是不执行任何操作。

我最初的想法是数据没有正确转义,但我不知道到底在哪里/如何转义它。所以我很困惑。

    localDB = null;

    function initDB()
    {
        if (localDB==null)
        {
            var shortName = 'Costcode';
            var version = '1.0';
            var displayName = 'Costcode';
            var maxSize = 217802; // in bytes
            localDB = window.openDatabase(shortName, version, displayName, maxSize);
        }

    }
            function buildTable()
    {
       var sQuery = 'CREATE TABLE IF NOT EXISTS Costcode ('+
                    'id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,' +
                    'cost_code_no VARCHAR NULL,' +
                    'row_unique_no VARCHAR NULL,' +
                    'cost_class_no VARCHAR NULL,' +
                    'Table_Version VARCHAR DEFAULT "1.0");';
        try
        {
            initDB();
            localDB.transaction(function(transaction)
            {
                transaction.executeSql(sQuery, []);
                console.log('sucess');
            });
        }
        catch (e)
        {
           alert("Error: Unable to create table 'x" + "' " + e + ".");
           return;
       }    
    }


 function exeSQLFast()
    {

                initDB();
                localDB.transaction(function(transaction)
                {
                    for (var x = 0; x <myJSONObject.costcodes.length; x++)
                    {

                        var costcodeno = myJSONObject.costcodes[x].cost_code_no;
                        var rowuniqueid = myJSONObject.costcodes[x].row_unique_id;
                        var costclassno = myJSONObject.costcodes[x].cost_class_no;
                        console.log(costcodeno);
                        console.log(rowuniqueid); 
                        console.log(costclassno);
                    transaction.executeSql('INSERT INTO Costcode (cost_code_no, row_unique_id, cost_class_no) VALUES (? , ? , ?)',
                     [costcodeno,
                     rowuniqueid,
                     costclassno]
                     , function(transaction, results)
                        {
                            console.log(costcodeno);
                            console.log('hooray');
                        }

                    )};
                    }
            )}

</script>
</head>

<body onLoad="buildTable();">
<input type="button" onClick="exeSQLFast();" value='button'>
</body>
</html>

控制台日志显示变量都已正确定义,但它没有运行插入语句。有什么想法吗?

这是 myJSONObject.costcodes[2] 的示例

cost_class_no: "    3"
cost_code_no: "      1000"
row_unique_id: 335

这看起来是个问题不是吗...

最佳答案

The problem was that I called the row_unique_no column row_unique_id in the insert statement. Now I feel stupid.

But if anyone is curious how to populate a WebSQL database properly, this is how you do it. Just don't mistype the column names.

关于javascript - SQL 插入语句在 Javascript 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10251419/

相关文章:

javascript - 使用jquery显示一个div隐藏其他div

mysql - 复杂的 MySQL JOIN SELECT

javascript - 使用 Web API MS CRM 时出现 JSON 错误

javascript - 检查 Handsontable 中的空属性

javascript - Angularjs。杰森普。发送数据

javascript - 什么是 channel 文件?

php - MySql 查询两个表

mysql - 多次加入同一张 table

jquery - Fineuploader错误处理

javascript - _.each 的回调函数?