Ciao a tutti!
il codice che posto lo utilizzo in una applicazione web che utilizza javascript+html5+sqlite con il framework Phonegap...
Funziona tutto, ma ora la mia esigenza è cambiata, dovrei visualizzare il risultato di questa query in una label SELECT SUM(campo1) FROM tabella
<script type="text/javascript">
function ensureTableExists(tx){
tx.executeSql('CREATE TABLE IF NOT EXISTS tabella (id INTEGER PRIMARY KEY, campo1, campo2, campo3, campo4)');
}
/** Called when browser load this page*/
function init(){
document.addEventListener("deviceready", onDeviceReady, false);
}
var campo1Box = null;
var campo2Box = null;
var db = null;
var dataTable = null;
/** Called when phonegap javascript is loaded */
function onDeviceReady(){
var addButton = document.getElementById("add");
campo1Box = document.getElementById("campo1");
campo2Box = document.getElementById("campo2");
dataTable = document.getElementById("data-table");
db = window.openDatabase("contactDB", "1.0", "Contact Database", 1000000);//name,version,display name, size
addButton.addEventListener(
"click",
function(){
db.transaction(
//function sql statements
function (tx){
ensureTableExists(tx);
var campo1 = campo1Box.value;
var campo2 = campo2Box.value;
var sql = 'INSERT INTO tabella (campo1, campo2) VALUES ("'+campo1+'","'+campo2+'")';
tx.executeSql(sql);
},
//error callback
function (err){
alert("error callback "+err.code);
},
//success callback
function (err){
//alert("success callback "+err.code);
loadFromDB();
}
);
},
false);
loadFromDB();
}
function loadFromDB(){
db.transaction(
//function sql statements
function (tx){
ensureTableExists(tx);
tx.executeSql('SELECT * FROM tabella',
[],
function(tx, results){
var htmlStr="";
for(var index=0;index<results.rows.length;index++){
var item = results.rows.item(index);
htmlStr=htmlStr+"<tr><td>"+item.campo1+"</td><td>"+item.campo2+"</td><td><button onclick=\"deleteEntry('"+item.id+"');\">X</button></td></tr>";
}
dataTable.innerHTML=htmlStr;
}
,
function(err){
alert("Unable to fetch result from tabella Table");
}
);
},
//error callback
function (err){
alert("error callback "+err.code+" "+err.message);
},
//success callback
function (){
campo1Box.value="";
campo2Box.value="";
}
);
}
function deleteEntry(id){
db.transaction(
//function sql statements
function (tx){
ensureTableExists(tx);
tx.executeSql('Delete FROM tabella where id='+id);
},
//error callback
function (err){
alert("error callback "+err.code+" "+err.message);
},
//success callback
function (err){
//alert("success callback ");
loadFromDB();
}
);
}
</script>