From: <bri...@cs...> - 2007-10-23 11:51:06
|
Immanuel Normann wrote: > Hello, > > I want to use the insert function and try to follow the guide from > http://haskelldb.sourceforge.net/guide/insert.html > > My database has one table t1 with the following layout: > > +-------+----------+------+-----+---------+-------+ > | Field | Type | Null | Key | Default | Extra | > +-------+----------+------+-----+---------+-------+ > | c1 | int(11) | YES | | NULL | | > | c2 | char(32) | YES | | NULL | | > +-------+----------+------+-----+---------+-------+ > > Right now I am able to set up all such that I can send successfully a > select query with e.g.: > > select = do withDB $ do t <- table t1 > project (c1 << t!c1) > > But I fail with insert though I follow the instruction (of course > adapted to my table layout) as presented on that website: > > > testInsert = > do withDB $ (\db -> insert db t1 ((c1 << constant 3) # > (c2 << constant "foo"))) > > This yields the error: > The lambda expression `\ db -> ...' has one argument, > but its type `Query (Rel er)' has none > In the second argument of `($)', namely > `(\ db > -> insert db t1 ((c1 << (constant 3)) # (c2 << (constant "foo"))))' > In the expression: > withDB > $ (\ db > -> insert db t1 ((c1 << (constant 3)) # (c2 << (constant "foo")))) > In the expression: > do withDB > $ (\ db > -> insert db t1 ((c1 << (constant 3)) # (c2 << (constant "foo")))) > > In fact the types are: > (\db -> insert db t1 ((c1 << constant 3) # (c2 << constant "foo"))) :: Database -> IO () > withDB :: (Database.HaskellDB.Database.GetRec er vr) => Query (Rel er) -> IO [Record vr] > > So, how does insert work correctly? > > Thanks, > Immanuel Hi Immanuel, your withDB type looks strange. You say that it is: withDB :: GetRec er vr => Query (Rel er) -> IO [Record vr] This looks like it takes a query, runs it, and returns the results. That is, I expect that your withDB implementation is something like this: withDB :: GetRec er vr => Query (Rel er) -> IO [Record vr] withDB q = connect (... db connection arguments ...) (\db -> query db q) But you are then trying to use the same withDB function to do an insert, even though the withDB in itself does a query. What I would do instead is to have withDB be something like (not tested): withDB :: (Database -> IO a) -> IO a withDB f = connect (... db connection arguments ...) f Then your query example would be: select = do withDB $ \db -> query db $ do t <- table t1 project (c1 << t!c1) And the insert would be as you wrote it: testInsert = withDB $ \db -> insert db t1 ((c1 << constant 3) # (c2 << constant "foo")) Note that every use of withDB opens and closes a database connection,so in real programs, you want to make sure not to do this over and over for performance reasons. /Björn |