2012-05-05 11:06:30 -04:00
|
|
|
<!DOCTYPE HTML>
|
|
|
|
<html>
|
|
|
|
<head>
|
|
|
|
<meta name="viewport" content="width=320; user-scalable=no" />
|
|
|
|
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
|
|
|
|
|
2012-05-05 11:22:34 -04:00
|
|
|
<title>SQLitePlugin test</title>
|
|
|
|
|
2012-12-05 13:30:29 -05:00
|
|
|
<link rel="stylesheet" href="qunit-1.5.0.css" />
|
2012-05-05 11:06:30 -04:00
|
|
|
|
2012-12-05 13:30:29 -05:00
|
|
|
<script type="text/javascript" charset="utf-8" src="cordova-2.2.0.js"></script>
|
2012-05-05 11:06:30 -04:00
|
|
|
<script type="text/javascript" charset="utf-8" src="SQLitePlugin.js"></script>
|
2012-12-05 13:30:29 -05:00
|
|
|
<script type="text/javascript" charset="utf-8" src="qunit-1.5.0.js"></script>
|
2012-05-05 11:06:30 -04:00
|
|
|
|
|
|
|
<script>
|
|
|
|
|
2012-05-05 17:18:06 -04:00
|
|
|
document.addEventListener("deviceready", doTest, false);
|
2012-05-05 11:06:30 -04:00
|
|
|
|
2012-05-05 17:18:06 -04:00
|
|
|
function doTest() {
|
2012-05-05 11:22:34 -04:00
|
|
|
|
2012-12-05 13:52:52 -05:00
|
|
|
test("db transaction test", function() {
|
2012-05-05 11:22:34 -04:00
|
|
|
|
2012-12-17 16:23:40 -05:00
|
|
|
var db = window.sqlitePlugin.openDatabase({name: "Database"});
|
2012-05-05 11:06:30 -04:00
|
|
|
|
2012-05-05 11:27:18 -04:00
|
|
|
ok(!!db, "db object");
|
2012-05-05 11:22:34 -04:00
|
|
|
|
2012-05-05 17:52:49 -04:00
|
|
|
stop(9);
|
2012-05-05 11:22:34 -04:00
|
|
|
|
2012-05-05 11:27:18 -04:00
|
|
|
db.transaction(function(tx) {
|
2012-05-05 11:22:34 -04:00
|
|
|
|
2012-05-05 11:27:18 -04:00
|
|
|
start(1);
|
|
|
|
ok(!!tx, "tx object");
|
2012-05-05 11:22:34 -04:00
|
|
|
|
2012-05-05 11:27:18 -04:00
|
|
|
tx.executeSql('DROP TABLE IF EXISTS test_table');
|
|
|
|
tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
|
2012-05-05 11:06:30 -04:00
|
|
|
|
2012-05-05 11:27:18 -04:00
|
|
|
tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
|
2012-05-05 11:57:12 -04:00
|
|
|
start(1);
|
|
|
|
ok(!!tx, "tx object");
|
|
|
|
ok(!!res, "res object");
|
|
|
|
|
2012-05-05 11:27:18 -04:00
|
|
|
console.log("insertId: " + res.insertId + " -- probably 1");
|
|
|
|
console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
|
2012-05-05 11:57:12 -04:00
|
|
|
|
2012-12-05 13:52:52 -05:00
|
|
|
ok(!!res.insertId, "Valid res.insertId");
|
2012-12-05 13:34:09 -05:00
|
|
|
// equal(res.rowsAffected, 1, "res rows affected"); // issue with Android version
|
2012-05-05 11:57:12 -04:00
|
|
|
|
2012-05-05 11:27:18 -04:00
|
|
|
db.transaction(function(tx) {
|
2012-05-05 11:57:12 -04:00
|
|
|
start(1);
|
|
|
|
ok(!!tx, "second tx object");
|
|
|
|
|
2012-05-05 17:52:49 -04:00
|
|
|
tx.executeSql("SELECT count(id) as cnt from test_table;", [], function(tx, res) {
|
2012-05-05 11:57:12 -04:00
|
|
|
start(1);
|
|
|
|
|
2012-05-05 11:27:18 -04:00
|
|
|
console.log("res.rows.length: " + res.rows.length + " -- should be 1");
|
|
|
|
console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
|
2012-05-05 11:57:12 -04:00
|
|
|
|
|
|
|
equal(res.rows.length, 1, "res rows length");
|
|
|
|
equal(res.rows.item(0).cnt, 1, "select count");
|
2012-05-05 11:27:18 -04:00
|
|
|
});
|
2012-05-05 16:25:05 -04:00
|
|
|
|
2012-12-05 14:29:51 -05:00
|
|
|
tx.executeSql("SELECT * from test_table;", [], function(tx, res) {
|
2012-05-05 17:52:49 -04:00
|
|
|
start(1);
|
|
|
|
|
|
|
|
equal(res.rows.length, 1, "SELECT res rows length");
|
2012-12-05 14:29:51 -05:00
|
|
|
equal(res.rows.item(0).data_num, 100, "SELECT res data_num");
|
|
|
|
equal(res.rows.item(0).data, "test", "SELECT res data");
|
2012-05-05 17:52:49 -04:00
|
|
|
});
|
|
|
|
|
2012-05-05 16:25:05 -04:00
|
|
|
tx.executeSql("UPDATE test_table SET data_num = ? WHERE data_num = 100", [101], function(tx, res) {
|
|
|
|
start(1);
|
|
|
|
|
|
|
|
console.log("UPDATE rowsAffected: " + res.rowsAffected + " -- should be 1");
|
|
|
|
|
2012-12-05 13:34:09 -05:00
|
|
|
// equal(res.rowsAffected, 1, "UPDATE res rows affected"); // issue with Android version
|
2012-05-05 16:25:05 -04:00
|
|
|
});
|
|
|
|
|
2012-05-05 17:52:49 -04:00
|
|
|
tx.executeSql("SELECT data_num from test_table;", [], function(tx, res) {
|
|
|
|
start(1);
|
|
|
|
|
|
|
|
equal(res.rows.length, 1, "SELECT res rows length");
|
|
|
|
equal(res.rows.item(0).data_num, 101, "SELECT data_num");
|
|
|
|
});
|
|
|
|
|
2012-05-05 16:25:05 -04:00
|
|
|
tx.executeSql("DELETE FROM test_table WHERE data LIKE 'tes%'", [], function(tx, res) {
|
|
|
|
start(1);
|
|
|
|
|
|
|
|
console.log("DELETE rowsAffected: " + res.rowsAffected + " -- should be 1");
|
|
|
|
|
2012-12-05 13:34:09 -05:00
|
|
|
// equal(res.rowsAffected, 1, "DELETE res rows affected"); // issue with Android version
|
2012-05-05 16:25:05 -04:00
|
|
|
});
|
|
|
|
|
2012-05-05 17:52:49 -04:00
|
|
|
tx.executeSql("SELECT data_num from test_table;", [], function(tx, res) {
|
|
|
|
start(1);
|
|
|
|
|
|
|
|
equal(res.rows.length, 0, "SELECT res rows length");
|
|
|
|
});
|
|
|
|
|
2012-05-05 11:06:30 -04:00
|
|
|
});
|
|
|
|
|
2012-05-05 11:27:18 -04:00
|
|
|
}, function(e) {
|
|
|
|
console.log("ERROR: " + e.message);
|
|
|
|
});
|
2012-05-05 11:06:30 -04:00
|
|
|
});
|
2012-05-05 11:22:34 -04:00
|
|
|
|
|
|
|
});
|
2012-05-05 11:57:12 -04:00
|
|
|
|
2012-12-05 13:52:52 -05:00
|
|
|
test("nested transaction test", function() {
|
|
|
|
|
2012-12-06 15:18:46 -05:00
|
|
|
var db = window.sqlitePlugin.openDatabase("Database", "1.0", "Demo", -1);
|
2012-12-05 13:52:52 -05:00
|
|
|
|
|
|
|
ok(!!db, "db object");
|
|
|
|
|
|
|
|
stop(3);
|
|
|
|
|
|
|
|
db.transaction(function(tx) {
|
|
|
|
|
|
|
|
start(1);
|
|
|
|
ok(!!tx, "tx object");
|
|
|
|
|
|
|
|
tx.executeSql('DROP TABLE IF EXISTS test_table');
|
|
|
|
tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)');
|
|
|
|
|
|
|
|
tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100], function(tx, res) {
|
|
|
|
start(1);
|
|
|
|
|
|
|
|
console.log("insertId: " + res.insertId + " -- probably 1");
|
|
|
|
console.log("rowsAffected: " + res.rowsAffected + " -- should be 1");
|
|
|
|
|
|
|
|
ok(!!res.insertId, "Valid res.insertId");
|
|
|
|
// equal(res.rowsAffected, 1, "res rows affected"); // issue with Android version
|
|
|
|
|
|
|
|
tx.executeSql("select count(id) as cnt from test_table;", [], function(tx, res) {
|
|
|
|
start(1);
|
|
|
|
|
|
|
|
console.log("res.rows.length: " + res.rows.length + " -- should be 1");
|
|
|
|
console.log("res.rows.item(0).cnt: " + res.rows.item(0).cnt + " -- should be 1");
|
|
|
|
|
|
|
|
equal(res.rows.length, 1, "res rows length");
|
|
|
|
equal(res.rows.item(0).cnt, 1, "select count");
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2012-12-05 16:22:04 -05:00
|
|
|
test("pragma & multiple databases", function() {
|
2012-12-06 15:18:46 -05:00
|
|
|
var db = window.sqlitePlugin.openDatabase("DB1", "1.0", "Demo", -1);
|
2012-12-05 16:22:04 -05:00
|
|
|
|
2012-12-06 15:18:46 -05:00
|
|
|
var db2 = window.sqlitePlugin.openDatabase("DB2", "1.0", "Demo", -1);
|
2012-12-05 16:22:04 -05:00
|
|
|
|
|
|
|
db.transaction(function(tx) {
|
|
|
|
tx.executeSql('DROP TABLE IF EXISTS test_table');
|
|
|
|
tx.executeSql('CREATE TABLE IF NOT EXISTS test_table (id integer primary key, data text, data_num integer)', [], function() {
|
|
|
|
console.log("test_table created");
|
|
|
|
});
|
|
|
|
|
|
|
|
stop();
|
|
|
|
db.executePragmaStatement("pragma table_info (test_table);", function(res) {
|
|
|
|
start();
|
|
|
|
console.log("PRAGMA res: " + JSON.stringify(res));
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
stop(2);
|
|
|
|
db2.transaction(function(tx) {
|
|
|
|
tx.executeSql('DROP TABLE IF EXISTS tt2');
|
|
|
|
tx.executeSql('CREATE TABLE IF NOT EXISTS tt2 (id2 integer primary key, data2 text, data_num2 integer)', [], function() {
|
|
|
|
console.log("tt2 created");
|
|
|
|
});
|
|
|
|
|
|
|
|
db.executePragmaStatement("pragma table_info (test_table);", function(res) {
|
|
|
|
start();
|
|
|
|
console.log("PRAGMA (db) res: " + JSON.stringify(res));
|
|
|
|
equal(res[0].name, "id", "DB1 table key field name");
|
|
|
|
equal(res[1].name, "data", "DB1 table text field name");
|
|
|
|
equal(res[2].name, "data_num", "DB1 table number field name");
|
|
|
|
});
|
|
|
|
|
|
|
|
db2.executePragmaStatement("pragma table_info (tt2);", function(res) {
|
|
|
|
start();
|
|
|
|
console.log("PRAGMA (tt2) res: " + JSON.stringify(res));
|
|
|
|
equal(res[0].name, "id2", "DB2 table key field name");
|
|
|
|
equal(res[1].name, "data2", "DB2 table text field name");
|
|
|
|
equal(res[2].name, "data_num2", "DB2 table number field name");
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
2012-05-05 11:06:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
</head>
|
|
|
|
<body>
|
2012-05-05 11:22:34 -04:00
|
|
|
<div id="qunit"></div>
|
2012-05-05 11:06:30 -04:00
|
|
|
</body>
|
|
|
|
</html>
|