sqlitehaskellhdbcscotty

Retrieving values from SQLite3 Database for Scotty


I'm trying to get information from a SQLite DB (HDBC.sqlite3) to feed to a web view using the Scotty framework. I'm currently trying to complete a "grab all" or rather select all the information from the table and then return it to display on my web page that is running via Scotty. I've encountered an error and I'm having some trouble figuring out how to fix it.

Here is my error:

Controllers/Home.hs:42:44:
Couldn't match expected type `Data.Text.Lazy.Internal.Text'
            with actual type `IO [[(String, SqlValue)]]'
In the expression: getUsersDB
In the first argument of `mconcat', namely
  `["<p>/users/all</p><p>", getUsersDB, "</p>"]'
In the second argument of `($)', namely
  `mconcat ["<p>/users/all</p><p>", getUsersDB, "</p>"]'

Here is my code:

import Control.Monad
import Web.Scotty (ScottyM, ActionM, get, html, param, text)
import Data.Monoid (mconcat)
import Controllers.CreateDb (createUserDB)
import Database.HDBC
import Database.HDBC.Sqlite3
import Control.Monad.Trans ( MonadIO(liftIO) )
import Data.Convertible

getAllUsers :: ScottyM()
getAllUsers = get "/users/all" $ do
  html $ mconcat ["<p>/users/all</p><p>", getUsersDB , "</p>"]

getUsersDB = do
  conn <- connectSqlite3 databaseFilePath
  stmt <- prepare conn "SELECT name FROM users VALUES"
  results <- fetchAllRowsAL stmt
  disconnect conn
  return (results)

Solution

  • run returns the number of rows modified : https://hackage.haskell.org/package/HDBC-2.4.0.0/docs/Database-HDBC.html#v:run