Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 97 lines (71 sloc) 3.622 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" id="viewport" content="height=device-height,width=device-width,user-scalable=no" />
<title>Database test</title>
<script type="text/javascript">
var db = null;

function startapp(){

// create/open database
db = openDatabase("databasetest", '1.0', 'Test', 50 * 1024);
                

                if (db) {

                    db.transaction(function (tx) {
// If table not exsist create it
                        tx.executeSql("CREATE TABLE IF NOT EXISTS userdata1 (key int unique, value1 text, value2 text, value3 text)");
// Get values from table
                        tx.executeSql("SELECT value1,value2,value3 FROM userdata1 WHERE key = ?", [1], function (ax, results) {
//Check record. If zero application will not work
                            if (results.rows.length == 1) {
                                document.getElementById("text1").value = results.rows.item(0).value1;
                                document.getElementById("text2").value = results.rows.item(0).value2;
document.getElementById("text3").value = results.rows.item(0).value3;
                            }
                            else {
// If zero create record with blank values
                                tx.executeSql("INSERT INTO userdata1 (key, value1, value2, value3) VALUES (?, ?, ?, ?)", [1, "", "", ""]);

                            }

                            
                        });
});
}
}

function savedata() {
//get values and put them into variables
var data1 = document.getElementById("text1").value;
var data2 = document.getElementById("text2").value;
var data3 = document.getElementById("text3").value;

db.transaction(function(bx){
//put values into database
bx.executeSql("UPDATE userdata1 SET value1 = ? WHERE key = ? ", [data1, 1]);
bx.executeSql("UPDATE userdata1 SET value2 = ? WHERE key = ? ", [data2, 1]);
bx.executeSql("UPDATE userdata1 SET value3 = ? WHERE key = ? ", [data3, 1]);
});

}

function loaddata() {

db.transaction(function (tx) {

                        tx.executeSql("CREATE TABLE IF NOT EXISTS userdata1 (key int unique, value1 text, value2 text, value3 text)");
                        tx.executeSql("SELECT value1,value2,value3 FROM userdata1 WHERE key = ?", [1], function (ax, results) {

                            if (results.rows.length == 1) {
                                document.getElementById("text1").value = results.rows.item(0).value1;
                                document.getElementById("text2").value = results.rows.item(0).value2;
document.getElementById("text3").value = results.rows.item(0).value3;
                            }
                            else {
                                tx.executeSql("INSERT INTO userdata1 (key, value1, value2, value3) VALUES (?, ?, ?, ?)", [1, "", "", ""]);

                            }

                        });
});

}

</script>
</head>
<body onload="startapp();">

<form>
<input type="text" id="text1"><br>
<input type="text" id="text2"><br>
<input type="text" id="text3"><br>
<input type="button" id="save" value="Save" onclick="savedata();"><br>
<input type="reset"><br>
<input type="button" id="read" onclick="loaddata();" value="Load">
</form>


</body>
</html>



Something went wrong with that request. Please try again.