It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

Display SQL results in a table using a scriptrunner macro

Hi,

I'm trying to display the results from an SQL on an external dabase in a table using a scriptrunner macro. I've added the external DB as a resource in scriptrunner. When running a custom script macro like this:

import com.onresolve.scriptrunner.db.DatabaseUtil
def SqlList = DatabaseUtil.withSql('iTrackDB') { sql ->
sql.rows('SELECT TOP 5 [ID],[user_name] FROM <tablename>')}

I get this result when using that macro on a page:

[{ID=<ID1>, user_name=<user1>}, {ID=<ID2>, user_name=<user2}, {ID=<ID3>, user_name=<user3>}, {ID=<ID4>, user_name=<user4>}, {ID=<ID5>, user_name=<user5>}, , ]

So the connection works fine, but how can I present this result as a table?

1 answer

1 accepted

1 vote
Answer accepted

I do something like this:

import com.onresolve.scriptrunner.db.DatabaseUtil

def rows = DatabaseUtil.withSql('iTrackDB') { sql ->
sql.rows('SELECT TOP 5 [ID],[user_name] FROM <tablename>')
}

def sqlTable = new StringBuffer()
//make a header row from the set of keys
sqlTable << """<table class="aui"><tr>"""
rows .first().keySet().each{
sqlTable << """<th>$it</th>"""
}
sqlTable << "</tr>"

//now get each sql row and create a table row
rows.each{
columns ->
sqlTable << "<tr>"
//look in each columna dn create a td element
columns.each{ cell ->
sqlTable << "<td>$cell.value</td>"
}
sqlTable << "</tr>"

}
return sqlTable

Fantastic, just what I wanted.

Hi Peter

I get errors trying exactly your code. Do you know why?

 


import com.onresolve.scriptrunner.db.DatabaseUtil

def rows = DatabaseUtil.withSql('test_cn') { sql ->
sql.rows('select COD_ACCESS, NAME From myTable')}


def sqlTable = new StringBuffer()
//make a header row from the set of keys
sqlTable << """<table class="aui"><tr>"""
rows .first().keySet().each{
sqlTable << """<th>$it</th>"""
}
sqlTable << "</tr>"

//now get each sql row and create a table row
rows.each{ columns ->
sqlTable << "<tr>"
//look in each columna dn create a td element
columns.each{ cell ->
sqlTable << "<td>$cell.value</td>"
}
sqlTable << "</tr>"

}
return sqlTable

 

 

TYgrafik.png

Static Type checking errors are informational only. The code will still work.

This is just because the code editor is unable to guess the data type of the rows variable.

Suggest an answer

Log in or Sign up to answer
TAGS
Community showcase
Posted in Confluence

How is your team having fun and bonding, remotely, utilizing Confluence?

Thanks everyone for answering last week’s question. The winner of the random drawing from those who commented is: @LarryBrock I’ll contact you separately with your prize details. This wee...

283 views 9 7
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you