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

前端 未结 3 996
不知归路
不知归路 2020-12-10 00:00

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.



        
相关标签:
3条回答
  • 2020-12-10 00:21

    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.

    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

    SELECT * FROM Employ where number = temp;
    
    0 讨论(0)
  • 2020-12-10 00:39
    '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;"
    
    0 讨论(0)
  • 2020-12-10 00:42

    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.

    0 讨论(0)
提交回复
热议问题