mysql-haskell-nem-0.1.0.0: Adds a interface like mysql-simple to mysql-haskell.

Safe HaskellNone
LanguageHaskell2010

Database.MySQL.Nem

Contents

Synopsis

Documentation

Extracting results

The queryResults and queryStmtResults functions return a list of values in the QueryResults typeclass. This class performs automatic extraction and type conversion of rows from a query result.

Here is a simple example of how to extract results:

import qualified Data.Text as Text
import Database.MySQL.Nem
import qualified System.IO.Streams as Streams

xs <- queryResults conn "select name,age from users"
Streams.mapM_
   \(name,age) ->
      putStrLn $ Text.unpack name ++ " is " ++ show (age :: Int)
   xs

Notice two important details about this code:

  • The number of columns we ask for in the query template must exactly match the number of elements we specify in a row of the result tuple. If they do not match, a ResultError exception will be thrown.
  • Sometimes, the compiler needs our help in specifying types. It can infer that name must be a Text, due to our use of the unpack function. However, we have to tell it the type of age, as it has no other information to determine the exact type.

For converting to your custom data types. Check the documentation for the QueryResults package.

queryResults :: QueryResults r => MySQLConn -> Query -> IO (InputStream r) Source #

Execute a MySQL query which return a result-set converted to the specified haskell type

Note that you must fully consumed the result-set before start a new query on the same MySQLConn, or an UnconsumedResultSet will be thrown. if you want to skip the result-set, use skipToEof.

queryStmtResults :: QueryResults r => MySQLConn -> StmtID -> [MySQLValue] -> IO (InputStream r) Source #

Execute prepared query statement with parameters, expecting resultset.

Note that you must fully consumed the result-set before start a new query on the same MySQLConn, or an UnconsumedResultSet will be thrown. if you want to skip the result-set, use skipToEof.