-2

Haskell:数据库.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"
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

4

1 回答 1

0

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

import Database.MySQL.Base

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

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

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 现在有了一个完整的主体。

于 2018-06-18T02:20:46.033 回答