Read lines with Slick 1.0.0

I am trying to create a query with Slick 1.0.0 that returns the number of rows equivalent to the following SQL statement:

SELECT COUNT(*) FROM table; 

What I still have:

 val query = for { row <- Table } yield row println(query.length) 

scala.slick.ast.FunctionSymbol$$anon$1@6860991f . In addition, query.length is of type scala.slick.lifted.Column . I cannot find a way to fulfill the request. All the examples that I can find in the documentation and elsewhere do not work on Column or ScalaQuery and no longer work.

What can I do to accomplish this?

+8
scala slick
source share
3 answers

Any of these should do the trick:

 Query(MyTable).list.length 

or

 (for{mt <- MyTable} yield mt).list.length 

or

 (for{mt <- MyTable} yield mt.count).first 

Update:

Printing the H2 database log shows this for the last query, which looks optimal:

  03:31:26.560 [main] DEBUG h2database - jdbc[2] /**/PreparedStatement prep10 = conn1.prepareStatement("select select count(1) from \"MYTABLE\" s5", 1003, 1007); 
+4
source share

Although I could not verify the resulting sql, you could get a shorter source by dropping the .list:

 Query(MyTable.length).first 
+4
source share

Using:

 val query = for(row <- Table) yield row println(Query(query.count).first) 

count equivalent to "SELECT COUNT (*) FROM Table". To get the first and only row, you must use first to get the score.

+2
source share

All Articles