The value is truncated in JSON object when the 

2019-06-08 15:25发布

问题:

I want to get the value from database using a SQL query (by sampleId which contains '-' sign), however, the query string is truncated.

Here is some of my script where the sampleId is fetched from the last API call:

   * json result = response[0].result
    * print result
    * def personId = result[0].personid
    * def sampleId = result[0].sampleid

Given path 'srehr/SendSample'

    * def config = read('classpath:utils/yntestDBConfig.json')
    * def DbUtils = Java.type('utils.DBUtils')
    * def db = new DbUtils(config)

    * def foo = {getBatchIDSQL: '#("select operatetime from sr_sendreceive_sample where sampleid = " + sampleId)'}
    * print foo.getBatchIDSQL

Here is the specified log in the report:

09:00:06.130 [print] select operatetime from sr_sendreceive_sample where sampleid = 1cfacfa4-eb06-4413-b060-9507bdebd1eb

mainFlow.feature:72 - javascript evaluation failed: db.readValue(foo.getBatchIDSQL), StatementCallback; bad SQL grammar [select operatetime from sr_sendreceive_sample where sampleid = 1cfacfa4-eb06-4413-b060-9507bdebd1eb]; nested exception is java.sql.SQLSyntaxErrorException: Unknown column '1cfacfa4' in 'where clause'

My question: from the log, you see that unknown column '1cfacfa4' is not expected, it should be '1cfacfa4-eb06-4413-b060-9507bdebd1eb'

回答1:

Try having the strings in SQL within quotes:

* def sql = "select operatetime from sr_sendreceive_sample where sampleid = '" + sampleId + "'"


标签: karate