Javascript 将变量插入 SQL 表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11232865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Inserting a Variable Into SQL Table
提问by Mitchell Byrd
I cannot seem to get user entered data into a table and then printed.
我似乎无法将用户输入的数据放入表格然后打印出来。
Here is my code thus far:
到目前为止,这是我的代码:
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript">
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
function submit() {
var input = document.getElementById("save_name").value;
localStorage.setItem("inputed_name", input);
var msg;
db.transaction(function (tx)
{
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id unique, log varchar(50))');
tx.executeSql('delete from LOGS'); // Clears table (for debugging)
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, localStorage.inputed_name)');
msg = '<p>Log message created and row inserted.</p>';
document.querySelector('#status').innerHTML = msg;
});
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM LOGS', [], function (tx, results) {
var len = results.rows.length, i;
msg = "<p>Found rows: " + len + "</p>";
document.querySelector('#status').innerHTML += msg;
for (i = 0; i < len; i++) {
msg = "<p><b>" + results.rows.item(i).log + "</b></p>";
document.querySelector('#status').innerHTML += msg;
}
}, null);
});
}
</script>
</head>
<body>
<div id="status" name="status">Status Message</div>
<p>Enter data: <input type="text" id="save_name" name="inputed_name" /><br/></p>
<p><button onclick="submit()" type="button">Submit Data</button></p> <!--Should print data from the table-->
</body>
</html>
I got the "template"/start for this from here
我从这里得到了“模板”/开始
Solved Below
已解决
Now I want to add two variables to my table, the original log variable and a second to act as a time stamp
现在我想在我的表中添加两个变量,原始日志变量和一个作为时间戳的第二个变量
function submit()
{
var input = document.getElementById("save_name").value;
var msg;
var time_stamp = new Date();
db.transaction(function (tx)
{
tx.executeSql('CREATE TABLE IF NOT EXISTS LOGS (id auto_increment, date_time varchar(128), log varchar(64))');
tx.executeSql('INSERT INTO LOGS (log, date_time) VALUES (?, ?)', [input, time_stamp]); // <--Problem here!
msg = '<p>Log message created and row inserted.</p>';
document.querySelector('#status').innerHTML = msg;
});
}
The rest of the code is the same as above. I would think this would work but when I test it nothing gets in the table.
其余代码与上述相同。我认为这会起作用,但是当我测试它时,表格中没有任何内容。
回答by C???
I think you just need to change the way you're getting and inserting your values (also, you had a syntax error in the SQL on this line):
我认为您只需要更改获取和插入值的方式(此外,您在此行的 SQL 中有语法错误):
var input = document.getElementById('someId').value;
...
tx.executeSql('INSERT INTO LOGS (id, log) VALUES (1, ?)', [input]);
...
I think you can forget about the localStoragevariable altogether, unless you need to persist those values.
我认为您可以localStorage完全忘记变量,除非您需要保留这些值。
回答by Adarsh V C
To insert multiple variables into the table, use the following format.
要将多个变量插入表中,请使用以下格式。
tx.executeSql('INSERT INTO CacheRequest (key,value) VALUES (?,?)',[variableKey,variableValue]);

