在wxHaskell中显示数据库内容

时间:2010-05-12 08:09:23

标签: haskell wxhaskell

我正在使用来自wxHaskell的教程,并希望在网格中显示表格电影的内容。 HEre是我的代码:

{--------------------------------------------------------------------------------
   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行)中获取rif错误

1 个答案:

答案 0 :(得分:3)

错误消息显示appendColumns g期望值[String]的值,但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