0

I am using node.js to connect to a DB2 database and loading different queries. But I would like to use parameters from user input. My connection string is:

var ibmdb = require("ibm_db")
    , cn = "DATABASE=namedb;HOSTNAME=hst;UID=portal;PWD=portalpwd;PORT=50000;PROTOCOL=TCPIP;"
    ;

But my query is static, I need to pass parameters/variables from user input.

I am using user input with var rl = require('readline'); but the problem now is how to communicate variables to this query and put dynamic parameters not a single value like name, id etc.

 var rows = conn.querySync(
   "select name,id,uid,password,type from db21.rep_name fetch first 10 rows only"
 );
1
  • Have you tried reading documentation? Specifically, about the bindingParameters argument to querySync()? Commented Jan 31, 2017 at 22:24

1 Answer 1

1

The Node.js package ibm_db is fully documented. There are several ways you could solve the problem, depending on whether you want to have the async or sync version and whether you want to first prepare, then execute the statement.

The simplest option probably is to use querySync as already done by you. There is an optional parameter bindingParameters to pass in an array of values. Those are bound to all places having a ?. Something like the following should work.

var rows = conn.querySync(
   "select name,id,uid,password,type from db21.rep_name where name=? fetch first 10 rows only", ['henrik']
 );
Sign up to request clarification or add additional context in comments.

1 Comment

select * from db21.rep_name where customer=? fetch first 10 rows only", ['customer']) --- i need a user input and put result in customer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.