[Takusen] working out how to use bind variables

Gregory Crosswhite gcross at phys.washington.edu
Wed Aug 11 18:21:42 EDT 2010


 The problem is that you are promising that "main" is of type "IO ()",
but the last expression in main is

    withSession connection (execDML (cmdbind "insert into activities
(Activity, Cost) values (?, ?)" [bindP foo, bindP bar]))

which is of type "IO Int", since execDML returns the number of rows that
were modified.  Thus, main is actually of type "IO Int", violating your
type constraint.

Cheers,
Greg


On 08/11/10 11:45, Michael Litchard wrote:
> Here's some toy code I have been playing with. The first few attempts
> work fine, but the final line, where I try to use bind variables,
> becomes a problem.
>
>
>
>> import Database.PostgreSQL.Enumerator
>> import Database.Enumerator
>> main :: IO ()
>> main = do
>> let connection = connect [CAdbname "tutorialDB"]
>> let foo = "test"
>> let bar = 42
>> withSession connection (execDDL (sql "create table activities (Activity char(20) PRIMARY KEY, cost int)"))
>> withSession connection (execDDL (sql "create table students (Student char (20), ID int PRIMARY KEY)"))
>> withSession connection (execDDL (sql "create table participants (Activity char (20) REFERENCES activities , ID int PRIMARY KEY)"))
>> withSession connection (execDDL (sql "insert into activities (Activity,Cost) values ('golf', 27)"))
>> withSession connection (execDML (cmdbind "insert into activities (Activity, Cost) values (?, ?)" [bindP foo, bindP bar]))
>
> tutorial.lhs:16:26:
>     Couldn't match expected type `()' against inferred type `Int'
>       Expected type: DBM mark Session ()
>       Inferred type: DBM mark Session Int
>     In the second argument of `withSession', namely
>         `(execDML
>             (cmdbind
>                "insert into activities (Activity, Cost) values (?, ?)"
>                [bindP foo, bindP bar]))'
>     In the expression:
>         withSession
>           connection
>           (execDML
>              (cmdbind
>                 "insert into activities (Activity, Cost) values (?, ?)"
>                 [bindP foo, bindP bar]))
>
>
> I know this means I need to fix the type, but I don't know how. I've
> been looking at the code used for testing, plus the usage examples
> from Enumerator.lhs. Haven't quite got it. Any help pointing me in the
> right direction would be much welcome.
>
> Michael Litchard
>
> _______________________________________________
> Takusen mailing list
> Takusen at projects.haskell.org
> http://projects.haskell.org/cgi-bin/mailman/listinfo/takusen




More information about the Takusen mailing list