I'd like to execute some raw sql to my db. I've be...
# exposed
c
I'd like to execute some raw sql to my db. I've been looking through the issues on the git repo, and have found some useful comments here and here. But when I try to write the sql string, the compiler complains with the message
id, name or spec expected, got blah
. I don't understand why I'm getting this error. Can anyone help?
a
You can use the
exec
method within the transaction. Here is an example:
Copy code
transaction {
    exec("SELECT countrycodes.id FROM countrycodes") { rs ->
        while (rs.next()) {
            println(rs.getString("id"))
        }
    }
}
c
That still gives me the same error. Thanks though. 👍
a
You have to pass a block as the second argument. Does it work if you copy the entire snippet?
c
I decided to revert to using jdbc, so I could make some progress. I'll bear that in mind when I try again. Thanks
c
@Chris An extra thought:
exec()
uses
@Language("sql")
on its
stmt
parameter to enable (generic sql) language injection for code assistance in the IDE. Assuming that you're using IntelliJ, if you've configured the SQL dialect to match your datasource and you're confident that the SQL statement is valid for that dialect, then the error may be related to this bug: KTIJ-24766
👀 1
c
Thanks @Chantal Loncle, I'm am using Intellij. I am just getting started with connecting my database to my ktor server so am not really confident about much. I did have a search for the error message before posting here but nothing came up. Maybe this is indeed what I'm seeing, For the time being I'll stick with jdbc to get things up and running but will return to Exposed at some point and will bear this in mind also. 👍
👍 1