SQLite3 haskell createFunction example

817 views Asked by At

Here's the SQLite3 Haskell bindings with the ability to create function: http://hackage.haskell.org/packages/archive/sqlite/0.5.1/doc/html/Database-SQLite.html

But I can't get to use this feature, I wrote the code like this:

increment a = a + 1

checkout = do
       handle <- openConnection "test.db"
       ok <- createFunction handle "woot" (IsFunctionHandler increment)

       return $ execStatement handle "SELECT woot(5)";

But it isn't compile with "Not in scope: data constructor `IsFunctionHandler'" error


The correct code is:

module Test where

import Database.SQLite
import Int

increment :: Int64 -> Int64
increment a = a + 1

checkout :: IO (Either String [[Row Value]])
checkout = do
       handle <- openConnection "test.db"
       ok <- createFunction handle "woot" increment

       execStatement handle "SELECT woot(5), woot(7), woot(128)"

Thanks to HaskellElephant

1

There are 1 answers

1
HaskellElephant On BEST ANSWER

IsFunctionHandler is a class, not a data constructor. It has several instances so if increment is an instance of IsFunctionHandler, wich it in this case is, you should be able to write:

createFunction handle "woot" increment