当我尝试编译时,以下do块会抛出错误“输入`conn'上的解析错误”。我尝试过if-then-else语句的许多不同配置无济于事。在添加条件之前数据库逻辑工作,所以没有问题。我在其他地方有太多行吗?有没有办法解决这个问题而不完全修改逻辑?
main = do
contents <- BL.getContents
let myData = decode contents :: Maybe Data
if maybe True (\x -> result x /= "success") myData
then error ("JSON download failed")
else let myTrades = process myData
conn <- connectSqlite3 "trades.db"
insert <- DB.prepare conn "INSERT INTO trades VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"
DB.executeMany insert $ map (\xs -> map DB.toSql xs) myTrades
DB.commit conn
DB.disconnect conn
答案 0 :(得分:5)
您需要在do
之后引入else
块,如此:
else do let myTrades = process myData
conn <- connectSqlite3 "trades.db"
insert <- DB.prepare conn "INSERT INTO trades VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"
DB.executeMany insert $ map (\xs -> map DB.toSql xs) myTrades
DB.commit conn
DB.disconnect conn