How to remove square brackets from List results Groovy? - mysql

I have a query, the results of which I am adding to a list.
These results are being passed into another query using $systemTypeQueryResults
The value still has the square brackets around it which is causing the query to fail. My question is how to remove the square brackets in Groovy. If it helps the values are all integers. The population of the list works as follows:
String systemTypeQuery ="SELECT id from system_type where type = '${systemType}';"
def systemTypeQueryResults = []
ticketerDb.eachRow(systemTypeQuery) {
systemTypeQueryResults << it.id
}
When these results are used in a further query they display as follows: [1]
I would like to know how to remove these square brackets?

toString() on a list object produces the string result with the brackets, to avoid the brackets you can use join on the list object as follows:
String systemTypeQuery ="SELECT id from system_type where type = '${systemType}';"
def systemTypeQueryResults = []
ticketerDb.eachRow(systemTypeQuery) {
systemTypeQueryResults << it.id
}
def queryWithoutBrackets = systemTypeQueryResults.join(",");
This way you have a string with your results separated by commas which you can pass to your next query.
Hope this helps,

Related

Pattern matching using regex with Scala Anorm

I'm Using Scala(2.11) and playframework(2.3) and trying to run a query using a helper function to get results through pattern matching. The function is as follows
def resultsfunc() = {
val gradeRegex = "^Class 5\."
val currRegex = "\.NCERT$"
DB.withConnection{ implicit c =>
val filterQuery = SQL(
"""
select * from tbl_graphs
where graph_name REGEXP '{grade_regex}' and
graph_name REGEXP '{curr_regex}' and org_id = 4
""")
.on("grade_regex" -> gradeRegex,
"curr_regex" -> currRegex)
filterQuery().map{ graphRecord =>
new ResultObj(graphRecord[Long]("id"),
graphRecord[String]("name"))
}.toList
}
}
I don't get any errors but I get empty result even though there are multiple records that match the pattern. The same query works if I try to run in mysql workbench and when I tried to print filterQuery the arguments were also mapped correctly.
Should Pattern matching with regex must be carried out differently in Scala Anorm ?
It has absolutely nothing to do specifically with Anorm.
Make sure that executing manually the query with exactly the same data and parameter, you get result.
When using JDBC (even through Anorm), string parameter must not be quoted in the query statement (... '{grade_regex}' ...).
Since a long time, it's recommended to use Anorm interpolation (SQL"SELECT x FROM y WHERE z = ${v}") rather than SQL(..) function.

How to receive mysql query output as list instead of tuple?

I am using the following query to get a list of ids corresponding to the given parameters:
userids=[]
sql_select_query = """SELECT userid FROM database1 WHERE username = %s"""
cursor.execute(sql_select_query, (username,))
record = cursor.fetchall()
for row in record:
userids.append(row)
print(userids)
I get a result like:
[('1460871223475326979',), ('1460871240332238850',), ('1460871258518736898',), ('1460871271219085312',), ('1460871286180220941',), ('1460871308963680260',)]
I would like to get this result as a list without the brackets, quotes and braces as 1460871223475326979, 1460871240332238850, 1460871258518736898, 1460871271219085312, 1460871286180220941, 1460871286180220941, 1460871308963680260
I tried the
",".join(map(str,userids))
method, but it only removed the [ ] at the start and end and did nothing for the braces and quotes.
Try this:
You can perform indexing on the list of tuples. All you have to do is,
for row in record:
userids.append(row[0])
print(userids)
Indexing at zero returns only the first element of each tuple in the list.```

jsonPath return string without brackets

I have the following code:
responseBody = '[{"properties":{"gridProperties":{"columnCount":26,"rowCount":1000},"index":3,"sheetId":1682983530,"sheetType":"GRID","title":"kamasi"}}]';
def jsonPathSearch = "sheets[?(#.properties.title=='kamasi')].properties"
def foundString = JsonPath.read(responseBody, jsonPathSearch)
println foundString.sheetId;
but it returns [1682983530], not 1682983530. It this possible to get 1682983530 without having to get the first element of the array such as foundString.sheetId[0] ? I know that there will be definitely only one element since the title of the sheet has to be unique

How can I loop over a map of String List (with an iterator) and load another String List with the values of InputArray?

How can I iterate over a InputArray and load another input array with the same values except in lower case (I know that there is a string to lower function)?
Question: How to iterate over a String List with a LOOP structure?
InputArray: A, B, C
OutputArray should be: a, b, c
In case, you want to retain the inputArray as such and save the lowercase values in an outputArray, then follow steps in below image which is self explanatory:
In the loop Step, Input Array should be /inputArray and Output Array should be /outputArray.
Your InputArray field looks like a string field. It's not a string list.
You need to use pub.string:tokenize from the WmPublic package to split your strings into a string list and then loop through the string list.
A string field looks like this in the pipeline:
A string list looks like this in the pipeline:
See the subtle difference in the little icon at the left ?
I can see two cases out here.
If your input is a string
Convert the string to stringlist by pub.string:tokenize service.
Loop over the string list by providing the name of string list in input array property of loop.
within loop use pub.string:toLower service as transformer and map the output to an output string.
put the output string name in the output array property of Loop.
once you come out of the loop you will see two string lists, one with upper case and one with lower case.
If your input is a string list.
In this case follow steps 2 to 5 as mentioned above.

How to split this String in two parts?

I would like to split a string like this in Access 2000 (Visual Basic function):
"[Results]
[Comments]"
in two parts:
the results part
the comments part
As you can notice, these two parts are separated by an empty line (always, this is our separator).
[Results] and [Comments] are blocks of text. We don't care what's in it except:
the results part doesn't have any empty lines in it, so the first empty line we see is the separator one.
I want my function to extract the Comments part only.
Here is what i tried:
Public Function ExtractComm(txt As String) As String
Dim emptyLine As Integer
txt = Trim(txt)
'emptyLine = first empty line index ??
emptyLine = InStrRev(txt, (Chr(13) + Chr(10)) & (Chr(13) + Chr(10)))
'Comments part = all that is after the empty line ??
ExtractComm = Mid(txt, emptyLine + 4)
End Function
But it doesn't work well.
If I do:
ExtractComm(
"Res1
Res2
Comment1
Comment2"
)
I want to obtain:
"Comment1
Comment2"
but I only obtain Comment2. Any idea to extract the comment part ?
Thanks a lot !
Maybe you need to use InStr instead of InStrRev
InStrRev
Returns the position of the first occurrence of one string within another, starting from the right side of the string.
InStr
Returns an integer specifying the start position of the first occurrence of one string within another.