Sandeep Bonagiri
Sandeep Bonagiri

Reputation: 59

How to parameterize the SQL query in karate

I want to parameterize and the pass the column2 ='value' dynamically from feature file, can you help me in how to achieve this.

Below is the sql file intitation.sql:

SELECT column1, column2, column3, column4, column5, column6, column7, column8, column9, column10, column11, column12 FROM table1 WHERE **column2='value'**;

Upvotes: 1

Views: 2548

Answers (2)

Peter Thomas
Peter Thomas

Reputation: 58058

This is normal JS fundamentals:

* def value = 'foo'    
* def sql = "select * from dogs where name = '" + value + "'"

Also see replace if it helps: https://github.com/intuit/karate#replace

EDIT: also see https://stackoverflow.com/a/71063078/143475

Upvotes: 4

djangofan
djangofan

Reputation: 29669

Maybe this would also work? Just something to chew on as being an interesting solution. On more complicated parameterizations, this could work well.

* def String = Java.type('java.lang.String')
* def pString = "Select * from Whatever where id = '%s' and name = '%s'"
* def query = String.format(pString, "my-id", "my-name")

Also, could perhaps load the Java PreparedStatement class?

Upvotes: 2

Related Questions