Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <script>
- var db = null;
- try{
- db = openDatabase('Employee', '1.0', 'Employee', 2 * 1024 * 1024);
- }catch(ignore){
- console.error("local DB is not supported");
- }
- //This create the database table
- function createLocalDB(){
- if(db != null){
- db.transaction(
- function (transaction) {
- transaction.executeSql(
- '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);',
- [],
- function dataHandler(transaction, results){},
- function errorHandler(transaction, error){
- console.error("Error creating table: " + error.code + ": " + error.message);
- }
- );
- }
- );
- }
- }
- //This will save the data
- function SaveDB(){
- if(db != null){
- var hascar=false;
- var answer=prompt("Does the employee have a car? ","");
- if(answer=="yes"||answer=="Yes"){
- hascar=true;
- }
- var name=document.getElementById("Name").value;
- var id=document.getElementById("ID").value;
- var doj=document.getElementById("DOJ").value;
- alert("Comment is submited");
- db.transaction(
- function (transaction) {
- transaction.executeSql(
- 'INSERT INTO Employee (Emp_name, Emp_id, emp_DOJ,emp_HasCar) VALUES (?, ?, ?, ?);',
- [name,id,doj,hascar],
- function dataHandler(transaction, results){},
- function errorHandler(transaction, error){
- console.error("Error inserting into table: " + error.code + ": " + error.message);
- }
- );
- }
- );
- document.getElementById("Name").value="";
- document.getElementById("ID").value="";
- document.getElementById("DOJ").value="";
- }else{
- alert("Please fill in all the fields");
- return;
- }
- return -1;
- }
- //This will display the data
- function DisplayDB(){
- if(db != null){
- db.transaction(
- function (transaction) {
- //transaction.executeSql('DROP TABLE ' +'Employee');
- transaction.executeSql(
- 'select * from Employee;',
- [],
- function processResult(transaction, results){
- //console.log(results);
- var html="";
- var found=false;
- for(var i=0;i<results.rows.length;i++){
- if(document.getElementById("Search").value==results.rows[i].Emp_id){
- html+="<label><b>Emplyee Name: </b></label>"+"<label>"+ results.rows[i].Emp_name+"</label>";
- html+="<br/>"
- html+="<label><b>Emplyee ID: </b></label>"+"<label>"+ results.rows[i].Emp_id+"</label>";
- html+="<br/>"
- html+="<label><b>Emplyee DOJ: </b></label>"+"<label>"+ results.rows[i].emp_DOJ+"</label>";
- html+="<br/>"
- html+="<label><b>Emplyee Has Car: </b></label>"+"<label>"+ results.rows[i].emp_HasCar+"</label>";
- html+="<br/>"
- found=true;
- }
- }
- if(!found){
- alert("Employee wasnt found in database");
- }
- document.getElementById("Data").innerHTML=html;
- },
- function errorHandler(transaction, error){
- console.error("Error reading records: " + error.code + ": " + error.message);
- }
- );
- }
- );
- }
- }
- </script>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement