比较两个没有外键的表的两个SQL结果

时间:2015-07-08 15:07:56

标签: javascript html sql sqlite

我已经创建了一种(非常冗长的)比较JS中两个不同表的两个结果的方法。我希望如果在lates表中输入的名称已经存在于student表中,则会立即将其删除。问题在于我认为的getlist功能。

我知道这是一种做我需要的可怕方式,但这是目前需要完成的方式。



if (window.openDatabase) {
  var mydb = openDatabase("students2_db", "0.1", "A Database of Students", 1024 * 1024);

  mydb.transaction(function(t) {
    t.executeSql("CREATE TABLE IF NOT EXISTS student (id INTEGER PRIMARY KEY ASC, fname TEXT, lname TEXT, mclass TEXT, aclass TEXT, com TEXT, lates INTEGER DEFAULT 0)");
    t.executeSql("CREATE TABLE IF NOT EXISTS lates (lid INTEGER PRIMARY KEY ASC, flname TEXT, llname TEXT, time TEXT, reason TEXT, date TEXT, nextdet TEXT)");
  });

} else {
  alert("WebSQL is not supported by your browser!");
}


function updateList2(transaction, results) {
  var listitems = "";
  var listholder = document.getElementById("list2");
  listholder.innerHTML = "";

  var i;

  for (i = 0; i < results.rows.length; i++) {

    var row = results.rows.item(i);

    var llname = row.llname;
  }

  getlist(llname);

}

function updateList3(transaction, results) {

  var listitems = "";
  var listholder = document.getElementById("list3");
  listholder.innerHTML = "";

  var i;

  for (i = 0; i < results.rows.length; i++) {

    var row = results.rows.item(i);

    var lname = row.llname;
  }

  getlist(lname);

}


function getlist(lname, llname) {

  if (lname != llname) {
    mydb.transaction(function(t) {
      t.executeSql("DELETE FROM lates WHERE llname = ?", [llname]);
    });
  }
}


function searchlastname2() {
  var llname = document.getElementById("lastname").value;

  if (mydb) {

    mydb.transaction(function(t) {
      t.executeSql("SELECT DISTINCT llname FROM lates WHERE llname =?", [llname], updateList2);
    });
  } else {
    alert("db not found, your browser does not support web sql!");
  }
}

function searchlastnameStudent() {
  var lname = document.getElementById("lastname").value;

  if (mydb) {

    mydb.transaction(function(t) {
      t.executeSql("SELECT DISTINCT lname FROM student WHERE lname =?", [lname], updateList3);
    });
  } else {
    alert("db not found, your browser does not support web sql!");
  }
}


function ClearFunction() {
  document.getElementById("mora").value = "";
}

function DateFunction() {
  var today = new Date();
  var dd = today.getDate();
  var mm = today.getMonth() + 1; //January is 0!
  var yyyy = today.getFullYear();

  if (dd < 10) {
    dd = '0' + dd
  }

  if (mm < 10) {
    mm = '0' + mm
  }

  today = dd + '-' + mm + '-' + yyyy;
  document.getElementById('datelate').value = today;
}

function addLate() {

  if (mydb) {

    var flname = document.getElementById("firstname").value;
    var llname = document.getElementById("lastname").value;
    var date = document.getElementById("datelate").value;
    var nextdet = document.getElementById("detentiondate").value;
    var time = document.getElementById("mora").value;
    var reason = document.getElementById("reason").value;

    if (flname !== "" && date !== "" && nextdet !== "" && llname !== "" && reason !== "" && time !== "") {

      mydb.transaction(function(t) {
        t.executeSql("INSERT INTO lates (flname,llname,time,reason,date,nextdet) VALUES (?,?,?,?,?,?)", [flname, llname, time, reason, date, nextdet]);
        t.executeSql("UPDATE student SET lates = lates + 1 WHERE lname =? ", [llname]);
        alert("Entry succesfully added");
        document.getElementById("firstname").value = "";
        document.getElementById("lastname").value = "";
        document.getElementById("detentiondate").value = "";
        document.getElementById("reason").value = "";
      });
    } else {
      alert("You must fill out all the empty information!");
    }
  } else {
    alert("db not found, your browser does not support web sql!");
  }
}
&#13;
<fieldset>
  <legend><b>Details</b>
  </legend>
  <label>First Name</label>
  <input id="firstname" type="text" autofocus="" placeholder="Enter first name">
  <br>
  <br>
  <label>Last Name</label>
  <input id="lastname" type="text" autofocus="" placeholder="Enter last name">
  <br>
  <br>
</fieldset>

<fieldset>
  <legend><b>Information</b>
  </legend>
  <label>Current date:</label>
  <input type="text" id="datelate" />
  <br>
  <br>
  <label>Detention date:</label>
  <input id="detentiondate" type="date">
  <br>
  <br>
  <label>Time</label>
  <select id="mora">
    <option value="AM">Morning</option>
    <option value="PM">Afternoon</option>
  </select>
  <br>
  <br>
  <label>Reason:</label>
  <textarea id="reason" rows="2" cols="60"></textarea>
  <br>
  <br>
</fieldset>

<br>
<input type="reset" value="Reset">

<button type="button" id="addlate" onclick="addLate(); searchlastname2(); searchlastnameStudent();">Add late</button>

<p id="list2"></p>
<p id="list3"></p>
&#13;
&#13;
&#13;

0 个答案:

没有答案