带有mysql的Haskell错误加载模块

时间:2018-06-17 22:38:18

标签: haskell do-notation

Haskell:Database.MySQL.Base

insert_Producto  = do
    conn <- connect
    defaultConnectInfo {ciUser = "root", ciPassword = "", ciDatabase = "prueba"}
    oks <- executeMany conn "delete from producto"

加载模块:

Conexion.hs:27:5: error:
    The last statement in a 'do' block must be an expression
      oks <- executeMany conn "delete from producto"
   |
27 |     oks <- executeMany conn "delete from producto"
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

我尝试加载模块,但它标志着我的错误。 有谁知道这样做的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我的理解是这是你正在执行的代码:

import Database.MySQL.Base

insert_Producto = do
    conn <- connect (defaultConnectInfo {ciUser = "root", ciPassword = "", ciDatabase = "prueba"})
    oks <- executeMany conn "delete from producto" []

编译器抱怨,因为您将executeMany绑定到oks块的最后一行中的变量do。如果你想让你的do块返回executeMany的输出,你可以这样做:

insert_Producto1 = do
    conn <- connect (defaultConnectInfo {ciUser = "root", ciPassword = "", ciDatabase = "prueba"})
    executeMany conn "delete from producto" []

您的代码出现问题的原因是do符号被一系列绑定(>>=)所取代。您编写的代码将转换为

insert_Producto2 = connect (defaultConnectInfo {ciUser = "root"
                                               , ciPassword = ""
                                               , ciDatabase = "prueba"})
                       >>= \conn ->
                           executeMany conn "delete from producto" []
                               >>= \oks ->
                                   <???>

如您所见,最后一个lambda函数中没有正文,这会导致错误。我上面提到的代码块可能会出现:

insert_Producto1 = connect (defaultConnectInfo {ciUser = "root"
                                               , ciPassword = ""
                                               , ciDatabase = "prueba"})
                       >>= \conn ->
                           executeMany conn "delete from producto" []

最后一个lambda现在有一个完整的身体。