Is there an idiomatic way to select a single value from a SQL query?


Some SQL queries (like “SELECT COUNT(*)…”) return a single value. The shortest way I’ve managed to come up with for obtaining the result of the query is:

using SQLite
db = SQLite.DB()
SQLite.DBInterface.execute(db, "CREATE TABLE t (field TEXT)")
count = iterate(SQLite.DBInterface.execute(db, "SELECT COUNT(*) FROM t"))[1][1]

That last line is a bit of a mouthful. Is there are better way to do this?


DBInterface is exported from SQLite.jl, so you could do:

counts = first(first(DBInterface.execute(db, "SELECT COUNT(*) FROM t")))

you could also define this as a local function in our app/script if you’ll re-use it a lot:

count(db, tbl) = first(first(DBInterface.execute(db, "SELECT COUNT(*) FROM $tbl")))


I wasn’t too worried about the SQLite.DBInterface... bit, it was the iterate()[1][1], and first(first()) is nicer. Good to know there’s not a DBInterface.execute_but_get_me_one_result and I’ll wrap first(first()) in something a bit prettier.


The value -add provided by the Spring Framework’s JDBC abstraction This class executes SQL queries , update statements or stored procedure calls.

One Vanilla