感谢插件SQLitePlugin的提供者,插件地址:https://github.com/brodysoft/Cordova-SQLitePlugin。
下面是我的测试代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="format-detection" content="telephone=no" />
<!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
<meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
<link rel="stylesheet" type="text/css" href="css/index.css" />
<meta name="msapplication-tap-highlight" content="no" />
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/index.js"></script>
<script type="text/javascript">
// Wait for Cordova to load
document.addEventListener("deviceready", onDeviceReady, false);
var db;
// Cordova is ready
function onDeviceReady() {
//var db = window.sqlitePlugin.openDatabase("Database", "1.0", "Demo", -1);
db = window.sqlitePlugin.openDatabase({name: "my.db"});
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, name text, age integer)');
});
db.transaction(function(tx) {//主键自增长
console.log("事务:begiin");
console.log("插入insert:begin");
tx.executeSql('INSERT INTO test_table (name, age) VALUES (?,?)',["john",26]);
tx.executeSql('INSERT INTO test_table (name, age) VALUES (?,?)',["smith",27]);
console.log("插入insert:end");
console.log("查询insert:begin");
tx.executeSql("select id,name,age from test_table", [], function(tx, res) {
console.log("第一次:begiin");
for (var i = 0; i < res.rows.length; i++) {
console.log("查看结果:第一次: id="+res.rows.item(i).id+"; name=" +res.rows.item(i).name + "; age=" + res.rows.item(i).age);
}
console.log("第一次:begiin");
}, function(e) {
});
console.log("查询insert:end");
try{
jkdljlksjkld;
}catch(e){
}
tx.executeSql('update test_table set age=? where name=?',[266,"john"]);
tx.executeSql('update test_table set age=? where name=?',[267,"smith"]);
tx.executeSql("select id,name,age from test_table", [], function(tx, res) {
for (var i = 0; i < res.rows.length; i++) {
console.log("查看结果:第二次: id="+res.rows.item(i).id+"; name=" +res.rows.item(i).name + "; age=" + res.rows.item(i).age);
}
}, function(e) {
});
console.log("事务:end");
});
// setTimeout(function(){
db.transaction(function(tx) {
tx.executeSql("select id,name,age from test_table", [], function(tx, res) {
console.log("最终次是否到达?begin");
for (var i = 0; i < res.rows.length; i++) {
console.log("查看结果:最终次: id="+res.rows.item(i).id+"; name=" +res.rows.item(i).name + "; age=" + res.rows.item(i).age);
}
console.log("最终次是否到达?end");
}, function(e) {
});
});
db.transaction(function(tx) {
tx.executeSql('INSERT INTO test_table (id, name, age) VALUES (?,?,?)',[3,"帅哥",24]);
tx.executeSql('INSERT INTO test_table (id, name, age) VALUES (?,?,?)',[4,"美女",23]);
});
//},15000);
}
function selectMe(){
db.transaction(function(tx) {
console.log("查看结果:db begin");
tx.executeSql("select id,name,age from test_table", [], function(tx, res) {
for (var i = 0; i < res.rows.length; i++) {
console.log("查看结果:db: id="+res.rows.item(i).id+"; name=" +res.rows.item(i).name + "; age=" + res.rows.item(i).age);
}
}, function(e) {
});
console.log("查看结果:db end");
});
}
</script>
<title>sqlite</title>
</head>
<body>
<header>
头
</header>
<div class="content">
<input type="button" name="select" onclick="selectMe();" />
</div>
<footer>
尾
</footer>
</body>
</html>