引入cordova.js和SQLitePlugin.js
这些代码我写在了body中的最下面测试
<body onload="onDeviceReady()">
<p id = "count1"> </p>
<script>
document.addEventListener("deviceready", onDeviceReady,false);
function onDeviceReady() {
console.log("加载数据库");
var db=window.sqlitePlugin.openDatabase({name:'demo.db',location:'default'});
var msg;
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)');
tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test",100]);
tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["hello",200]);
tx.executeSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["friend",300],function(tx,res) {},function(e) {
alert("ERROR: "+e.message);
});
tx.executeSql("select * from test_table", [],function(tx,res) {
var le=res.rows.length;
var row=res.rows.item(0);
var countValue=row.data_num;
msg="<p>countValue "+countValue+""</p>;
document.querySelector('#count1').innerHTML=countValue;
});
});
}
</script>
</body>