-1

Haskell : Database.MySQL.Base

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

load Module :

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"
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

I try to load the module, but it marks me error. Does anyone know what would be the right way to do it?

melpomene
  • 84,125
  • 8
  • 85
  • 148

1 Answers1

0

My understanding is that this is the code you are executing:

import Database.MySQL.Base

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

The compiler is complaining because you bind executeMany to the variable oks in the final line of a do block. If you want your do block to return the output of executeMany, you could do this:

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

The reason why your code is problematic is that do notation is desugared to a series of binds (>>=). The code you wrote would be transformed to

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

As you can see, there is no body in the last lambda function, which causes the error. The code block I proposed above would desugar to:

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

The last lambda now had a complete body.

mhuesch
  • 31
  • 5
  • Thanks for your time answering, testing insert_Product2 throws me error: – yens solis asto Jun 18 '18 at 02:46
  • • Couldn't match expected type ‘IO a0’ with actual type ‘[p1] -> IO OK’ • Probable cause: ‘execute’ is applied to too few arguments In a stmt of a 'do' block: execute conn "delete from producto" In the expression: do conn <- connect defaultConnectInfo {ciUser = "root", ciPassword = "", ciDatabase = "prueba"} execute conn "delete from producto" close conn In an equation for ‘delete_Producto’: – yens solis asto Jun 18 '18 at 02:48
  • What is the type of `executeMany`? It looks like it takes an additional list argument, which hasn’t been supplied. – mhuesch Jun 18 '18 at 02:52
  • Esta es la sentencia: executeMany :: QueryParam p => MySQLConn -> Consulta -> [[p]] -> IO [OK] – yens solis asto Jun 18 '18 at 03:00
  • Ah, ok. So it takes a list of query parameters. As background context: the way to read that type signatures is that the 3 types to the left of the last `->` are arguments, and the type on the right is the return type. So this function needs 3 arguments to be fully applied. You can pass `[]` if you don’t have any query parameters. I’ve just updated the above code blocks. – mhuesch Jun 18 '18 at 03:08
  • Next message : • Ambiguous type variable ‘p0’ arising from a use of ‘executeMany’ prevents the constraint ‘(QueryParam p0)’ from being solved. Probable fix: use a type annotation to specify what ‘p0’ should be. These potential instances exist: instance QueryParam MySQLValue -- Defined in ‘mysql-haskell-0.8.3.0:Database.MySQL.Query’ instance QueryParam Param -- Defined in ‘mysql-haskell-0.8.3.0:Database.MySQL.Query’ – yens solis asto Jun 18 '18 at 03:14