Run sql statement programatically

Answered

Hello,

 

Is there a way to run an SQL statement programatically through database api?

I am trying to build a dialog panel where the user will choose several options, which will ultimately generate a query that will be executed (similar to how the "Modify table" dialog works). Now I got stuck at the executing the query part.

 

Any help is appreciated.

Thank you!

2 comments
Comment actions Permalink

Hi!

To run statements pragmatically you need DatabaseConnection object. To get the DatabaseConnection you can use DatabaseSessionManager.facade(...).runSync()/runAsync()/connect() methods. Next step is SmartStatements.poweredBy(connection).simple().exectue(..). So the code should look like:

DatabaseSessionManager.facade(args...).runAsync(conn -> {

  SmartStatements.poweredBy(conn).simple().execute("some sql", ...)

});

 

This is not the final API and can be changed in the future.

0
Comment actions Permalink

Hi,

Thank you so much for pointing me in the right direction. I had a little trouble identifying what all the parameters mean for DatabaseSessionManager.facade(...) method, but I was able to find a (probably dumb) workaround and make the code work.

For now, it seems that my code below works very well, but I have a feeling that all the missing / null parameters will backfire at some point.

0

Please sign in to leave a comment.