我有一个脚本,我需要检查用户名和密码,并使用sqlite事务显示一个消息。状态msg在正确的条目中显示相应的消息,但不会显示错误条目的警报消息。可以解决这个问题。脚本如下:
var db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS Contact1(id unique, username TEXT NOT NULL, pass TEXT NOT NULL)");
tx.executeSql("INSERT INTO Contact1(id, username, pass) VALUES (1, 'pp1', '321')");
// tx.executeSql('INSERT INTO LOGS (id, log) VALUES (2, "logmsg")');
});
$(document).ready(function () // Call function when page is ready for load..
{
db.transaction(function (tx) {
tx.executeSql("INSERT INTO Contact1(id, username, pass) VALUES (3, 'pp1', '321')");
});
$("#sub").click(function(){
var uname = $("#username").val();
var password = $("#password").val();
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM Contact1 WHERE username=?', [uname], function (tx, results) {
var len = results.rows.length, i;
// msg = "<p>Found rows: " + len + "</p>";
//document.querySelector('#status').innerHTML += msg;
for (i = 0; i < len; i++){
// msg = "<p><b>" + results.rows.item(i).pass + "</b></p>";
//document.querySelector('#status').innerHTML += msg;
if(results.rows.item(i).username == uname){
// alert("hello");
tx.executeSql('SELECT * FROM Contact1 WHERE pass=?', [password], function (tx, results) {
var len = results.rows.length, i;
msg = "<p>Found rows: " + len + "</p>";
document.querySelector('#status').innerHTML += msg;
for (i = 0; i < len; i++){
msg = "<p><b>" + results.rows.item(i).username + "</b>==><b>" + results.rows.item(i).pass + "</b></p>";
document.querySelector('#status').innerHTML += msg;
}
}, null);
}
else
{//wrong username/password case
msg="<p><b>Please enter the right Username and password"+"</b></p>";
document.querySelector('#status').innerHTML += msg;
}
}
}, null);
});});});
提前致谢:D
答案 0 :(得分:1)
根据您的代码
如果用户名是错误的,则返回len = 0,因此它不会进入else条件。
试试这个
$("#sub").click(function(){
var uname = $("#username").val();
var password = $("#password").val();
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM Contact1 ', function (tx, results) {
var len = results.rows.length, i;
for (i = 0; i < len; i++){
if(results.rows.item(i).username == uname &&results.rows.item(i).pass == password){
// alert("hello");
msg = "<p><b>" + results.rows.item(i).username + "</b>==><b>" + results.rows.item(i).pass + "</b></p>";
document.querySelector('#status').innerHTML += msg;
}
else
{
msg="<p><b>Please enter the right Username and password"+"</b></p>";
document.querySelector('#status').innerHTML += msg;
}
}
}, null);
答案 1 :(得分:1)
Suganthi指出了问题,但是我决定重写你的代码以解决问题并使代码更好:
var db;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
db = openDatabase('mydb', '1.0', 'Test DB', 2 * 1024 * 1024);
db.transaction(function (tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS Contact1(id unique, username TEXT NOT NULL, pass TEXT NOT NULL)");
tx.executeSql("INSERT INTO Contact1(id, username, pass) VALUES (1, 'pp1', '321')");
tx.executeSql("INSERT INTO Contact1(id, username, pass) VALUES (3, 'pp1', '321')");
});
}
$("#sub").click(function() {
var uname = $("#username").val();
var password = $("#password").val();
db.transaction(function (tx) {
tx.executeSql('SELECT * FROM Contact1 WHERE username = ? AND pass = ?', [uname, password], function (tx, results) {
var len = results.rows.length;
if (len > 0) {
$('#status').append("<p>Found rows: " + len + "</p>");
for (i = 0; i < len; i++) {
msg = "<p><b>" + results.rows.item(i).username + "</b>==><b>" + results.rows.item(i).pass + "</b></p>";
$('#status').append(msg);
}
} else {
msg = "<p><b>Please enter correct username and password</b></p>";
$('#status').append(msg);
}
}, null);
});
});