简体   繁体   中英

WebSQL - Check database for records (javascript)

I am working on a phonegap app, and have so far set-up the database to which I have the insert and delete function's working correctly.

I am now struggling on checking input values with the data in the database, so for example for now I am just simply attempting to check if any rows are returned, and if there is that will mean the user and pass entered are in the database, if no rows are returned then the user and pass is false.

I am not getting any errors from the sql statement (function errorCB), as well I did put an debug alert within the function LoginSuccess and that worked, however after removing the debug alert in the LoginSuccess function, I do not get prompted with any alerts and instead the page just refreshes.

I have removed the delete and insert functions from the code as it's relevant to this issue.

Any help will be much appreciated.

document.addEventListener("deviceready", onDeviceReady, false);

           var db;

             function onDeviceReady() {
              db = window.openDatabase("DBkhan", "1.0", "SFDatabase", 2*1024*1024);
                         db.transaction(createDB, errorCB, successCB);

                     }

            function createDB(tx) {
   tx.executeSql('CREATE TABLE IF NOT EXISTS mahdi (FirstName text, LastName text, Email text, Password text)');

                     }

                     function errorCB(err) {
                         alert("Error processing SQL: "+err.code);
                     }

                     function successCB() {
                  alert("Database Ready");
                     }

 function loginUser()
         {
        db = window.openDatabase("DBkhan", "1.0", "SFDatabase", 2*1024*1024);
        db.transaction(loginDB, errorCB, LoginSuccess);
        }

        function loginDB(tx)
        {
           var Username = document.getElementById("username").value;
         var Password = document.getElementById("password").value;
         tx.executeSql("SELECT * FROM mahdi WHERE FirstName='" + Username + "' AND Password= '" + Password + "'");

         } function LoginSuccess(tx, results) {
           if (results.rows.length > 0) {
           alert ("User and Pass Found");
           }
           else
           {
           alert ("User and Pass incorrect");
           }

}

Don't worry, all fixed. See below if you'd like to see what I changed around. Seem's as though it was the missing array [] in the executesql statement and calling the renderList (changed from loginSuccessful) into the exectutesql statementent.

       function loginDB(tx)
        {
        alert("yep yep yep");
           var Username = document.getElementById("username").value;
         var Password = document.getElementById("password").value;
         tx.executeSql("SELECT * FROM mahdi WHERE FirstName='" + Username + "' AND Password= '" + Password + "'", [], renderList);

         }
           function renderList(tx,results) {
           if (results.rows.length > 0) {
            navigator.notification.alert("User and Pass Found");
           }
           else
           {
           navigator.notification.alert("User and Pass incorrect");
           }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM