As a quick note today, here’s an example Play Framework Anorm SQL SELECT query that queries for a single value:
libraryDependencies ++= Seq( guice, "org.scalatestplus.play" %% "scalatestplus-play" % "3.1.2" % Test, jdbc, "mysql" % "mysql-connector-java" % "5.1.46", "com.typesafe.play" %% "anorm" % "2.5.3" )
In general the online Play Framework documentation is excellent, but one area where I needed more help was in seeing more examples of the Anorm syntax. To that end, here are some Anorm query examples, taken from a Play Framework application I worked on recently. But first, a little background about Anorm.
It’s embarrassing how long it took to get this to work, but if you ever need to select a single row from a database using Play Framework (version 2.6), Anorm, and
singleOpt, I hope this example is helpful, and will save you time and frustration:
I don’t know if there is a better way to do this yet, but if you want to select one record from a database table using the Play Framework and Anorm, I do know that this code works:
Here’s a quick look at how to run Play Framework “model” methods from the Play console. In my case, my model methods are Anorm database access methods, but you may be accessing MongoDB, Cassandra, CouchDB, whatever.
First, move to your Play application directory and start the Play interpreter:
Then start the Play console:
[MyApp] $ console
Your prompt will now look like this:
As a quick example, if you need to see some code for a Play Framework Anorm method that inserts an object into a relational database table and returns the auto-into (MySQL auto_increment) ID field on success, here you go: