如何在javascript中基于传递给函数的值从数据库获取数据 [英] How to get data from database in javascript based on the value passed to the function

查看:161
本文介绍了如何在javascript中基于传递给函数的值从数据库获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

 < script> 
//函数打开数据库并创建,将数据插入表

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>

在正文标签中,

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

我在从数据库检索数据时遇到问题,即在SELECT查询时,它不接受值parseInt(val),如果我给条件'name = 1'它只接受条件并给出结果,但它不接受'name = parseInt(val)'并抛出错误为'no such column:val code:1'
请告诉我如何传递值在'val'..
感谢在前进..

解决方案

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

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

 SELECT * FROM Employ where number = 10;


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.

<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 * FROM Employ where number = ' + parseInt(val, 10) + ';'

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

"SELECT * FROM Employ where number = 10;"

这篇关于如何在javascript中基于传递给函数的值从数据库获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆