Javascript 如何根据传递给函数的值从javascript中的数据库中获取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11027982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 04:06:26  来源:igfitidea点击:

How to get data from database in javascript based on the value passed to the function

javascriptdatabase

提问by user25

In my web application, I want to retrieve data from database based on the value that is passed to the function. I wrote the query as follows.

在我的 Web 应用程序中,我想根据传递给函数的值从数据库中检索数据。我按如下方式编写了查询。

<script> 
 //Functions to open database and to create, insert data into tables

 getSelectedRow = function(val)
    {
        db.transaction(function(transaction) {
              transaction.executeSql('SELECT * FROM Employ where number = parseInt(val);',[], selectedRowValues, errorHandler);

        });
    };
    selectedRowValues = function(transaction,results)
    {
         for(var i = 0; i < results.rows.length; i++)
         {
             var row = results.rows.item(i);
             alert(row['number']);
             alert(row['name']);                 
         }
    };
</script>

In body tag,

在身体标签中,

<body>
   ---
     <a href = "#" onClick = "javascript:getSelectedRow('1')>getValues</a>

I got the problem while retrieving data from database, i.e., at SELECT query. It doesn't accept the value parseInt(val), if I give the condition as 'name = 1' it simply accepts the condition and give the result, but it doesn't accept 'name = parseInt(val)' and throws error as 'no such column: val code: 1' Please tell me how to pass value in 'val'.. Thanks in Advance..

我在从数据库中检索数据时遇到了问题,即在 SELECT 查询中。它不接受值 parseInt(val),如果我将条件指定为 'name = 1' 它只是接受条件并给出结果,但它不接受 'name = parseInt(val)' 并抛出错误如“没有这样的列:val 代码:1”请告诉我如何在“val”中传递值.. 提前致谢..

采纳答案by Esailija

'SELECT * FROM Employ where number = ' + parseInt(val, 10) + ';'

For example, if valis "10"then this will end up building the string:

例如,如果val"10"那么这将最终构建字符串:

"SELECT * FROM Employ where number = 10;"

回答by Ankit

The error is coming as your query is getting formed as

当您的查询形成为

SELECT * FROM Employ where number = parseInt(val);

I dont know which DB you are using but no DB will understand parseInt.

我不知道您使用的是哪个数据库,但没有数据库会理解parseInt

What you can do is use a variable say temp and store the value of parseInt(val)in temp variable and make the query as

您可以做的是使用一个变量说 temp 并将其值存储parseInt(val)在 temp 变量中并使查询为

SELECT * FROM Employ where number = temp;

回答by DThought

Try the following:

请尝试以下操作:

<script> 
 //Functions to open database and to create, insert data into tables

 getSelectedRow = function(val)
    {
        db.transaction(function(transaction) {
              transaction.executeSql('SELECT * FROM Employ where number = ?;',[parseInt(val)], selectedRowValues, errorHandler);

        });
    };
    selectedRowValues = function(transaction,results)
    {
         for(var i = 0; i < results.rows.length; i++)
         {
             var row = results.rows.item(i);
             alert(row['number']);
             alert(row['name']);                 
         }
    };
</script>

You don't have access to javascript variable names in SQL, you must pass the values to the Database.

您无权访问 SQL 中的 javascript 变量名称,您必须将值传递给数据库。