如何在SQLite中将变量值插入我的数据库?

时间:2014-01-19 22:02:19

标签: javascript sqlite

我设法使用带有以下代码的文本字段id插入文本字段的内容,但是宁愿将文本字段中的任何内容分配给变量,然后插入该变量的值。

var newName = document.getElementById("txNewName").value;
var ing1 = document.getElementById("txIngredient1").value;
var ing2 = document.getElementById("txIngredient2").value;
var ing3 = document.getElementById("txIngredient3").value;

db.transaction(function(transaction) {
    transaction.executeSql('INSERT INTO MyRecipes(MyRecipeName, MyIngredient1, MyIngredient2, MyIngredient3) VALUES (?,?,?,?)',[$('#txNewName').val(), $('#txIngredient1').val(), $('#txIngredient2').val(), $('#txIngredient3').val()]);
});

我正在使用SQLite,如果它有所作为,并且我是一个非常新的,所以将不胜感激任何帮助。

1 个答案:

答案 0 :(得分:0)

在给定的代码中,您的变量在不同的范围内声明 - 因此在函数中,值不会被知道。您需要使用全局变量,或将它们作为参数传递。下面我更改了答案,将它们放在函数中。这可能不是你想要的,但它会确认问题出在哪里:

db.transaction(function(transaction) {
    var newName = document.getElementById("txNewName").value;
    var ing1 = document.getElementById("txIngredient1").value;
    var ing2 = document.getElementById("txIngredient2").value;
    var ing3 = document.getElementById("txIngredient3").value;
console.log('Inserting into the database ' + newName + ',' + ing1 +',' + ing2 + ',' + ing3);
    transaction.executeSql('INSERT INTO MyRecipes(MyRecipeName, MyIngredient1, MyIngredient2, MyIngredient3) VALUES (?,?,?,?)',[newName, ing1, ing2, ing3]);
});