简体   繁体   中英

Node.js DB2 query with parameters

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"
 );

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']
 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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