<meta charset="utf-8"><span class="Apple-style-span" style="border-collapse: collapse; font-family: arial, sans-serif; font-size: 13px; ">Hello, beginner here, looking for some ideas on how to simplify how I use the Takusen database library to query a database and return types.<br>
<br>In the working code below, I'm able to query the database and through several functions get a list of simple types. I've tried playing around with the query2Iteratee and getAccounts' functions to simplify the code and reduce it to a single function, but keep getting hung up on compiler errors due to type issues.<br>
<br>Is there a better way to write this, or is it best to keep the various functions quite separate? (in my real project, I have the database access in a separate module from the list of custom types and the code to convert to the Account type).<br>
<br>Thanks in advance,<br>Neil<br><br>import Database.Sqlite.Enumerator<br>import Database.Enumerator<br><br>data Account = Account {account_id :: Int}<br> deriving (Eq, Show)<br><br><br>query2Iteratee :: (Monad m) => Int -> [Int] -> m (IterResult [Int])<br>
--query2Iteratee :: (Monad m) => Int -> IterAct m [Int]<br>query2Iteratee a accum = result' (a:accum)<br><br><br>getAccounts' :: IO [Int]<br>getAccounts' = do<br> let dbh = connect<br>"performance_data.sqlite"<br>
withSession dbh (do<br> r <- doQuery (sql "SELECT account_id FROM<br>ACCOUNT;") query2Iteratee []<br> return r<br> )<br><br><br>getAccounts :: IO [Account]<br>
getAccounts = do<br> accountsList <- getAccounts'<br> let r = map conv accountsList<br> return r<br><br>conv :: Int -> Account<br>conv a = Account {account_id=a}<br><br><br>
main = do<br> accounts <- getAccounts'<br> putStrLn (show accounts)<br><br></span>