View SQL query in Slick
Slick 2.X:
You can print the query statement as shown on the Slick documentation:
val invoker = q.invoker
val statement = q.selectStatement
For other type of statements look at insertStatement
, deleteStatement
and updateStatement
.
Slick 3.X:
val res = table.filter(_.id === 1L).result
res.statements.foreach(println)
Docs.
How to send plain SQL queries (and retrieve results) using scala slick 3
I realized I was missing a Seq in the return type:
def getData(TableName: String): Future[Seq[(Int,Double,String)]] = {
db.run(sql"""SELECT * FROM $TableName """.as[(Int, Double, String)])
}
Rewrite this SQL query using scala slick
The embedded style of Slick doesn't support dynamic mutating batch updates of the form SET position = position + 1
(Slick issue 497).
That means using Plain SQL style you already have is the right approach here.
An andditional note on idiomatic sqlu
: I notice the sqlu
you have uses "splicing" (#$
for the table name). That prevents Slick from treating the value as a string, which is appropriate for what you're doing. But the later usage #$previousPosition
would be more typically written as a plain substitution, $previousPosition
. That's because you'd typically want a SQL parameter there to be correctly escaped by Slick. It probably won't make a difference for integers, but could for other data types.
How to pass an array to a slick SQL plain query?
The issue you linked points to a commit which adds this:
def mkArraySetParameter[T: ClassTag](/* ... */): SetParameter[Seq[T]]
def mkArrayOptionSetParameter[T: ClassTag](/* ... */): SetParameter[Option[Seq[T]]]
Note that they are not implicit
.
You'll need to do something like
implicit val setIntArray: SetParameter[Array[Int]] = mkArraySetParameter[Int](...)
and make sure that is in scope when you try to construct your sql"..."
string.
How do I print the results of a slick query
You have written a Query, but it needs converted into an Action by calling its result method
val query = materials.map(_.name)
val action = query.result
val results: Future[ Seq[Option[ String ] ]] = db.run( action)
results.foreach( println )
The db object needs to be initialized depending the Slick version that you are using . e.g Slick or Play Slick
I assume that you have this
val materials = TableQuery[Materials]
Related Topics
Differencebetween '->>' and '->' in Postgres SQL
Set Database from Single User Mode to Multi User
Returning Month Name in SQL Server Query
How to Load SQL Fixture in Django for User Model
Cte to Traverse Back Up a Hierarchy
T-SQL Conditional Where Clause
SQL Count Total Number of Rows Whilst Using Limit
Is Possible to Reuse Subqueries
Using Same Column Multiple Times in Where Clause
SQL Server: Invalid Column Name
SQL Server Invalid Column Name After Adding New Column
How to Add a Not Null Column Without Default Value
Does the Order of Columns Matter in a Group by Clause
How to Return Second Newest Record in SQL
Postgresql Recursive Self Join
Oracle "Ora-01008: Not All Variables Bound" Error W/ Parameters