I'm trying to create a web site that will get information via URL routes and then pass this information into a HDBC SQLITE3
database. I have figured out how to get the information via parameters using Scotty as well as how to create a database using HDBC. But I'm having trouble passing the parameter information to the database. I'm getting this error: (sorry for such a beginner problem, I've only been using Haskell for about three weeks)
Controllers/Home.hs:51:48:
No instance for (Convertible a0 SqlValue)
arising from a use of `toSql'
Possible fix:
add an instance declaration for (Convertible a0 SqlValue)
In the expression: toSql
In the expression: toSql $ userId
In the third argument of `run', namely
`[toSql $ userId, toSql $ name]'
Controllers/Home.hs:51:64:
No instance for (Convertible a1 SqlValue)
arising from a use of `toSql'
Possible fix:
add an instance declaration for (Convertible a1 SqlValue)
In the expression: toSql
In the expression: toSql $ name
In the third argument of `run', namely
`[toSql $ userId, toSql $ name]'
Here is the code I'm trying to run:
import Control.Monad
import Web.Scotty (ScottyM, ActionM, get, html, param)
import Data.Monoid (mconcat)
import Database.HDBC
import Database.HDBC.Sqlite3
import Control.Monad.Trans ( MonadIO(liftIO) )
import Data.Convertible
createUser :: ScottyM()
createUser = get "/create/user/:userId/:name" $ do
name <- param "name"
userId <- param "userId"
liftIO $ createUserDB name userId
html $ mconcat ["<p>/create/user/" , userId , "/" , name ,"</p>"]
createUserDB :: a1 -> a0 -> IO()
createUserDB name userId = do
conn <- connectSqlite3 databaseFilePath
run conn "INSERT INTO users VALUES (? , ?)" [toSql $ userId, toSql $ name]
commit conn
disconnect conn
databaseFilePath = "data/mydb.db"
If someone could provide how to fix this, as well as why their fix works that'd be really helpful!