<html>
<script>
var g_db = null;
if (typeof window.openDatabase == "undefined") {
document.write("Error: Web SQL databases are not supported.");
}
try {
g_db = openDatabase("test", "1.0", "test database", 1024 * 1024);
} catch(err) {
document.write("Error: cannot open database.");
}
function createTable() {
if (!g_db)
return;
g_db.transaction(
function(tx) {
tx.executeSql("CREATE TABLE table1 (data TEXT)");
},
function(error) {
sendValueToTest(error);
},
function() {
sendValueToTest("done");
});
}
function insertRecord(text) {
g_db.transaction(
function(tx) {
tx.executeSql("INSERT INTO table1 VALUES (?)", [text]);
},
function(error) {
sendValueToTest(error);
},
function() {
sendValueToTest("done");
});
}
function updateRecord(index, text) {
var didUpdate = false;
findId(index, function(rowId) {
g_db.transaction(
function(tx) {
tx.executeSql(
"UPDATE table1 SET data=? WHERE ROWID=?",
[text, rowId],
function(tx, result) {
if (result.rowsAffected == 1)
didUpdate = true;
else if (result.rowsAffected == 0)
sendValueToTest("could not update index: " + index);
else
sendValueToTest("multiple rows with index: " + index);
});
},
function(error) {
sendValueToTest("update error: " + error);
},
function() {
if (didUpdate)
sendValueToTest("done");
});
});
}
function deleteRecord(index) {
findId(index, function(rowId) {
g_db.transaction(
function(tx) {
tx.executeSql("DELETE FROM table1 WHERE ROWID=?", [rowId]);
},
function(error) {
sendValueToTest("delete error: " + error);
},
function() {
sendValueToTest("done");
});
});
}
function getRecords() {
g_db.readTransaction(function(tx) {
tx.executeSql(
"SELECT data FROM table1 ORDER BY ROWID",
[],
function(tx, result) {
items = "";
for (var i = 0; i < result.rows.length; i++) {
if (items != "")
items += ", ";
items += result.rows.item(i).data;
}
sendValueToTest(items);
},
function(tx, error) {
sendValueToTest("getRecords error: " + error);
});
});
}
function findId(index, callback) {
g_db.readTransaction(function(tx) {
tx.executeSql(
"SELECT ROWID AS id FROM table1 ORDER BY ROWID LIMIT ?,1",
[index],
function(tx, result) {
if (result.rows.length >= 1)
callback(result.rows.item(0).id);
else
sendValueToTest("could not find row with index: " + index);
},
function(tx, error) {
sendValueToTest("findId error: " + error);
});
});
}
function sendValueToTest(value) {
window.domAutomationController.setAutomationId(0);
window.domAutomationController.send(value);
}
</script>
<body>
This page is used for testing Web SQL databases.
</body>
</html>