我有如下代码将数据插入TestDB数据库,我可以成功插入和删除所有行,但删除一行不起作用。
请告诉我代码有什么问题。我认为问题来自函数 deleteRow 无法识别 column1 值。
谢谢
<script>
var db = window.openDatabase("Test", "1.0", "Test DB", 1000000);
var column1= document.getElementById("column1");
var column2= document.getElementById("column2");
var column3= document.getElementById("column3");
var results = document.getElementById("results");
var id = document.getElementById('id');
var Delete = document.getElementById('Delete');
function InsertDB(){
if ((column1.value!='') && (column2.value!='') && (column3.value!='')) {
db.transaction(function(tx) {
tx.executeSql('CREATE TABLE IF NOT EXISTS TestDB (id INTEGER, column1 TEXT, column2 TEXT, column3 TEXT)');
tx.executeSql('INSERT INTO TestDB (column1, column2, column3) VALUES (?, ?, ?)', [column1.value, column2.value, column3.value], ShowandReset);
});
}
else alert("Column cannot be empty");
}
function ShowDB() {
results.innerHTML = '';
db.transaction(function(tx) {
tx.executeSql("SELECT * FROM TestDB", [], function(tx, result) {
dataset = result.rows;
for (var i = 0, item = null; i < dataset.length; i++) {
item = dataset.item(i);
results.innerHTML += item['column1'] + ' , ' + item['column2'] + ' , ' + item['column3'] + ' ' + '<a href="#" onclick="deleteRow('+item['column1']+')"> Delete this row</a>' + '</br>';
}
});
});
}
function deleteRow(a) {
db.transaction(function(tx) {
tx.executeSql("DELETE FROM TestDB WHERE column1=a");
});
ShowDB();
}
function DeleteDB() {
db.transaction(function(tx) {
tx.executeSql("DELETE FROM TestDB");
});
ShowDB();
}
function resetForm(){
column1.value = '';
column2.value = '';
column3.value = '';
id.value = '';
}
function ShowandReset(){
resetForm();
ShowDB();
}
function LoadRow(i) {
var item = dataset.item(i);
column1.value = item['column1'];
column2.value = item['column2'];
column3.value = item['column3'];
id.value = item['id'];
}
</script>