A Play Framework Anorm SQL SELECT query that queries for a single value

As a quick note today, here’s an example Play Framework Anorm SQL SELECT query that queries for a single value:

def urlAliasExists(uri: String): Boolean = db.withConnection { implicit c =>
    val q: SimpleSql[Row] = SQL"""
        select count(dst) from url_alias
        where dst = $uri
    """
    val count = q.as(SqlParser.scalar[Int].single)
    if (count > 0) true else false
}

As shown, the SELECT query gets the count of a value from a database table, then does a little work in addition to that. But the purpose of this example is to show an example of the Anorm syntax for a query that returns a single value, so I hope it’s helpful in that way.

Add new comment

The content of this field is kept private and will not be shown publicly.

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.