我正在尝试创建一个网站,该网站将通过 URL 路由获取信息,然后将此信息传递到HDBC SQLITE3
数据库中。我已经弄清楚如何使用 Scotty 通过参数获取信息以及如何使用 HDBC 创建数据库。但是我无法将参数信息传递给数据库。我收到此错误:(对于这样的初学者问题,我很抱歉,我只使用 Haskell 大约三周)
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]'
这是我要运行的代码:
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"
如果有人可以提供如何解决这个问题,以及为什么他们的修复工作真的很有帮助!