简体   繁体   English

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

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

In my web application, I want to retrieve data from database based on the value that is passed to the function. 在我的Web应用程序中,我想基于传递给函数的值从数据库检索数据。 I wrote the query as follows. 我写的查询如下。

<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, ie, at SELECT query. 从数据库中检索数据时出现问题,即在SELECT查询中。 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.. 它不接受值parseInt(val),如果我将条件指定为“ name = 1”,则它仅接受条件并给出结果,但不接受“ name = parseInt(val)”并抛出错误如“无此类列:val代码:1”,请告诉我如何在“ val”中传递值。

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

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

"SELECT * FROM Employ where number = 10;"

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;

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变量名称,必须将值传递给数据库。

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

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