SQL插入语句不能在Javascript中工作 [英] SQL Insert Statement not Working in Javascript

查看:171
本文介绍了SQL插入语句不能在Javascript中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



该函数在单击时运行,数据库成功创建,但数据未插入数据库。它甚至没有抛出错误,它只是没有做任何事情。



我最初的想法是数据没有正确转义,但我没有确切知道在何处/如何逃避它。所以我很难过。

  localDB = null; 

函数initDB()
{
if(localDB == null)
{
var shortName ='Costcode';
var version ='1.0';
var displayName ='Costcode';
var maxSize = 217802; //以字节为单位
localDB = window.openDatabase(shortName,version,displayName,maxSize);
}

}
函数buildTable()
{
var sQuery ='CREATE TABLE IF NOT 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(错误:无法创建表'x+'+ e +。);
返回;
}
}


函数exeSQLFast()
{

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

VAR costcodeno = myJSONObject.costcodes [X] .cost_code_no;
变种rowuniqueid = myJSONObject.costcodes [X] .row_unique_id;
变种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]
,函数(交易,结果)
{
的console.log(costcodeno);
console.log('hooray');
}

)};
}
)}

< / script>
< / head>

< body onLoad =buildTable();>
< input type =buttononClick =exeSQLFast();值=按钮>
< / body>
< / html>

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



以下是 myJSONObject.costcodes [2]


$的示例b $ b

  cost_class_no:3
cost_code_no:1000
row_unique_id:335

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

解决方案

< blockquote>

问题是我在insert语句中调用了row_unique_no列row_unique_id。现在我觉得很蠢。



但如果有人好奇如何正确填充WebSQL数据库,那就是你这样做的。只是不要错误输入列名。



I'm trying to get my WebSQL database to popluate using a JSON array (Object is called myJSONObject, array is called costcodes).

The function runs on click, the database successfully creates, but the data is not inserted into the database. It doesn't even throw and error, it just doesn't do anything.

My initial thought was that the data isn't escaped properly, but I don't know exactly where/how to escape it. So I'm stumped.

    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>

The console log shows that the variables are all being properly defined, but it's not running the insert statement. Any ideas?

Here's an example of myJSONObject.costcodes[2]

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

That looks like a problem doesn't it...

解决方案

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.

这篇关于SQL插入语句不能在Javascript中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆