如何在Karate DSL的SQL查询中仅提取值而非列名?
Extract Numeric Value from JDBC Query Result in Karate
Got it, let's fix this up for you! The core issue here is that your db.readRows() method returns a list of map objects—each map represents a single database row, with column names as keys. That's why printing productId directly shows the full map { "PRODUCT_ID": 68 } instead of just the numeric value you need.
Modified Solution Code
Here's the adjusted script to pull out the exact numeric value for your request:
@Scenario1 Scenario: Retrieve productId to update contract plan via JDBC def config = { username: 'mmultapp', password: 'Mmu1t@pp', url: 'jdbc:db2://edb2dev3.momentum.co.za:60022/MMULTTST', driverClassName: 'com.ibm.db2.jcc.DB2Driver' } def DbUtils = Java.type('wellness_core_utils.DbUtils') def db = new DbUtils(config) // Fetch rows (returns a list of row maps) def productRows = db.readRows('select PRODUCT_ID from MULTUSR1.PLANS order by PLAN_ID desc fetch first 1 rows only') // Extract the first row's PRODUCT_ID value def productId = productRows[0].PRODUCT_ID print productId // Now outputs just 68
Key Details
readRows()always returns a list (even when you fetch a single row). UsingproductRows[0]grabs the first (and only) row from the result set..PRODUCT_IDaccesses the value tied to that column key in the row map, giving you the raw numeric value required for your update request.
Optional: Add a Safety Check
To avoid null pointer exceptions if the query returns no rows, add an assertion before extracting the value:
// Ensure we got at least one row from the query assert productRows.size() > 0 : "Error: No product records found in the PLANS table"
This will make your test fail early with a clear message instead of breaking silently.
内容的提问来源于stack exchange,提问作者Koos
相关产品推荐
相关产品推荐

