· 8 years ago · May 16, 2018, 03:56 PM
1 <script>
2 var db = null;
3
4 try{
5 db = openDatabase('Employee', '1.0', 'Employee', 2 * 1024 * 1024);
6 }catch(ignore){
7 console.error("local DB is not supported");
8 }
9 //This create the database table
10 function createLocalDB(){
11 if(db != null){
12 db.transaction(
13 function (transaction) {
14 transaction.executeSql(
15 'CREATE TABLE IF NOT EXISTS Employee(Emp_name TEXT NOT NULL , Emp_id INTEGER NOT NULL PRIMARY KEY, emp_DOJ INTEGER NOT NULL, emp_HasCar BOOL NOT NULL);',
16 [],
17 function dataHandler(transaction, results){},
18 function errorHandler(transaction, error){
19 console.error("Error creating table: " + error.code + ": " + error.message);
20 }
21 );
22 }
23 );
24 }
25 }
26 //This will save the data
27 function SaveDB(){
28 if(db != null){
29 var hascar=false;
30
31 var answer=prompt("Does the employee have a car? ","");
32 if(answer=="yes"||answer=="Yes"){
33 hascar=true;
34 }
35
36
37
38 var name=document.getElementById("Name").value;
39 var id=document.getElementById("ID").value;
40 var doj=document.getElementById("DOJ").value;
41
42
43 alert("Comment is submited");
44 db.transaction(
45 function (transaction) {
46 transaction.executeSql(
47 'INSERT INTO Employee (Emp_name, Emp_id, emp_DOJ,emp_HasCar) VALUES (?, ?, ?, ?);',
48 [name,id,doj,hascar],
49 function dataHandler(transaction, results){},
50 function errorHandler(transaction, error){
51 console.error("Error inserting into table: " + error.code + ": " + error.message);
52 }
53 );
54 }
55 );
56 document.getElementById("Name").value="";
57 document.getElementById("ID").value="";
58 document.getElementById("DOJ").value="";
59 }else{
60 alert("Please fill in all the fields");
61 return;
62 }
63 return -1;
64 }
65 //This will display the data
66 function DisplayDB(){
67 if(db != null){
68 db.transaction(
69 function (transaction) {
70 //transaction.executeSql('DROP TABLE ' +'Employee');
71 transaction.executeSql(
72 'select * from Employee;',
73 [],
74 function processResult(transaction, results){
75
76 //console.log(results);
77 var html="";
78 var found=false;
79 for(var i=0;i<results.rows.length;i++){
80 if(document.getElementById("Search").value==results.rows[i].Emp_id){
81 html+="<label><b>Emplyee Name: </b></label>"+"<label>"+ results.rows[i].Emp_name+"</label>";
82 html+="<br/>"
83 html+="<label><b>Emplyee ID: </b></label>"+"<label>"+ results.rows[i].Emp_id+"</label>";
84 html+="<br/>"
85 html+="<label><b>Emplyee DOJ: </b></label>"+"<label>"+ results.rows[i].emp_DOJ+"</label>";
86 html+="<br/>"
87 html+="<label><b>Emplyee Has Car: </b></label>"+"<label>"+ results.rows[i].emp_HasCar+"</label>";
88 html+="<br/>"
89 found=true;
90 }
91
92 }
93 if(!found){
94 alert("Employee wasnt found in database");
95 }
96
97 document.getElementById("Data").innerHTML=html;
98
99 },
100 function errorHandler(transaction, error){
101 console.error("Error reading records: " + error.code + ": " + error.message);
102 }
103 );
104 }
105 );
106 }
107 }
108 </script>