2010-05-12 61 views
1

使用來自wxHaskell的教程並希望在網格中顯示錶格電影的內容。 這裏是我的代碼:在wxHaskell中顯示數據庫內容

{-------------------------------------------------------------------------------- 
    Test Grid. 
--------------------------------------------------------------------------------} 

module Main where 

import Graphics.UI.WX 
import Graphics.UI.WXCore hiding (Event) 

import Database.HDBC.Sqlite3 (connectSqlite3) 
import Database.HDBC 


main 
    = start gui 

gui :: IO() 
gui 
    = do f <- frame [text := "Grid test", visible := False] 

     -- grids 
     g <- gridCtrl f [] 
     gridSetGridLineColour g (colorSystem Color3DFace) 
     gridSetCellHighlightColour g black 
     appendColumns g (movies) -- Here is error: 

--  Couldn't match expected type `[String]' 
--  against inferred type `IO [[String]]' 


     --appendRows g (map show [1..length (tail movies)]) 
     --mapM_ (setRow g) (zip [0..] (tail movies)) 
     gridAutoSize g 

     -- layout 
     set f [layout := column 5 [fill (dynamic (widget g))] 
      ]  
     focusOn g 
     set f [visible := True] -- reduce flicker at startup. 
     return() 
    where 

    movies = do 
     conn <- connectSqlite3 "Spop.db" 
     r <- quickQuery' conn "SELECT id, title, year, description from Movie where id = 1" [] 
     let myResult = map convRow r 
     return myResult 

setRow g (row,values) 
    = mapM_ (\(col,value) -> gridSetCellValue g row col value) (zip [0..] values) 



{-------------------------------------------------------------------------------- 
    Library?f 
--------------------------------------------------------------------------------} 

gridCtrl :: Window a -> [Prop (Grid())] -> IO (Grid()) 
gridCtrl parent props 
    = feed2 props 0 $ 
    initialWindow $ \id rect -> \props flags -> 
    do g <- gridCreate parent id rect flags 
     gridCreateGrid g 0 0 0 
     set g props 
     return g 

appendColumns :: Grid a -> [String] -> IO() 
appendColumns g [] 
    = return() 
appendColumns g labels 
    = do n <- gridGetNumberCols g 
     gridAppendCols g (length labels) True 
     mapM_ (\(i,label) -> gridSetColLabelValue g i label) (zip [n..] labels) 

appendRows :: Grid a -> [String] -> IO() 
appendRows g [] 
    = return() 
appendRows g labels 
    = do n <- gridGetNumberRows g 
     gridAppendRows g (length labels) True 
     mapM_ (\(i,label) -> gridSetRowLabelValue g i label) (zip [n..] labels) 

convRow :: [SqlValue] -> [String] 
convRow [sqlId, sqlTitle, sqlYear, sqlDescription] = [intid, title, year, description] 
       where intid = (fromSql sqlId)::String 
        title = (fromSql sqlTitle)::String 
        year = (fromSql sqlYear)::String 
        description = (fromSql sqlDescription)::String 

我應該怎麼做才能在代碼中的錯誤上面(24日線)

回答

3

該錯誤消息說,appendColumns g預計[String]類型的值的RIF,但movies是類型IO [[String]]

所以,你需要解決兩件事情:

  1. movies是一個IO動作返回一個值,但你需要它返回的值。

    更換

    appendColumns g (movies) 
    

    movieList <- movies 
    appendColumns g movieList 
    

    (順便說一下,在第一行的括號呢?他們沒有做任何事情。)

  2. 你需要養活appendColumns g列表的字符串,但是你想給它一個字符串列表的列表。您需要將列表列表轉換爲字符串列表,或者您需要依次給每個字符串列表輸入appendColumns g

    我猜你想要後者,所以你會得到一行屏幕上的數據庫中的每一行。 (我不熟悉wxhaskell,所以我可能誤解了什麼appendColumns一樣。)

    movieList <- movies 
    mapM_ (appendColumns g) movieList 
    
+0

謝謝,現在它的偉大工程,但我有一個問題: 你怎麼知道,電影是一個IO行動? 爲什麼如果電影是IO動作,那麼 x < - 電影 x - isnt與電影類型相同? =和< - 有什麼區別我不明白:/ – gruber 2010-05-12 16:57:21

+0

閱讀http://blog.sigfpe.com/2006/08/you-could-have-invented-monads-and.html – dave4420 2010-05-12 21:26:31

相關問題