简体   繁体   English

在JavaScript变量中使用where子句

[英]Using where clause with javascript variable

Hello all this is my first question on stack overflow. 您好,这是我关于堆栈溢出的第一个问题。 I am developing an app with phonegap, with using webSQL and javascript. 我正在使用webSQL和javascript开发带有phonegap的应用程序。 In my App there is a login form which takes two inputs as: username and password. 在我的应用程序中,有一个登录表单,其中包含两个输入:用户名和密码。 when user fills these values and clicks on Login these values are checked in database table which have multiple rows. 当用户填写这些值并单击“登录”时,将在具有多行的数据库表中检查这些值。 for checking values I am using this query- 为了检查值,我正在使用此查询-

var email=document.getElementById("uname").value;
tx.executeSql('SELECT * FROM USERINF Where Email=email', [], MatchPass, errorCB1);

But the query is not working with where clause with a java script variable. 但是该查询不适用于带有Java脚本变量的where子句。 I have also tried many syntexes such as 我也尝试过许多语法,例如

where Email=@email
where Email="+email+"

and many more but they all not worked. 还有更多,但都没有用。

Is it possible to use a javascript variable with where clause?? 是否可以将JavaScript变量与where子句一起使用? If yes please tell me how. 如果是,请告诉我如何。 if not please suggest me any other way to accomplish the task. 如果不是,请建议我其他任何方式来完成任务。 thanks in advance. 提前致谢。

this is my full JavascriptCode 这是我的完整JavascriptCode

// JavaScript Document
var db=window.openDatabase("CakeViewer", "1.0", "Cake Viewer", 2*1024*1024);

function login()
{

db.transaction(matchcred)

function matchcred(tx)
{
    var eml=document.getElementById("uname").value;
     tx.executeSql('SELECT * FROM USERINF Where Email=eml', [], MatchPass, errorCB1); 
 //problem is in above line-- the query is not executed with variable where clause. But it works fine if I use where Email="abc@abc.com"

}


function MatchPass(tx, results)
{
    var orgnalPass=results.rows.item(0).Password;   
    var userinputedPass=document.getElementById("pass");

    if(orgnalPass==userinputedPass.value)
    {
        window.location.href='HomePage.html';
    }
    else
    {
        errorCB();
    }
}

function errorCB(tx,err)
{
    alert("User Name or Password is not valid !");
    document.getElementById("uname").value="";
    document.getElementById("pass").value="";
}

function errorCB1()
{
    alert("Query failed");
}

function errorCB2(tx,err)
{
    alert("errorCB2"+err);
}
}

The easiest way is to use a questionmark (?) to specify the variables. 最简单的方法是使用问号(?)指定变量。

For example: 例如:

tx.executeSql('SELECT Data from Table Where something = ?', [email], MatchPass, errorCB1);

The ?'s get parsed from the left to the right and match the variables between [] from left to right. 从左到右解析?,并从左到右匹配[]之间的变量。

Another example: 另一个例子:

tx.executeSql('SELECT Data from Table Where email = ? AND username=?', [email, username], MatchPass, errorCB1);
tx.executeSql("SELECT * FROM USERINF Where Email='"+email+"'" , 

To get the value of the js variable email in the query you need to use + operator to concatenate. 要在查询中获取js变量电子邮件的值,您需要使用+运算符进行串联。 Also wrap the value with quotes as value may be string 还要用引号引起来的值,因为值可能是字符串

tx.executeSql('SELECT Data from Table Where something = "'+ email+ '"', [], MatchPass, errorCB1);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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