| Safe Haskell | None |
|---|---|
| Language | Haskell2010 |
Database.MySQL.Nem
Contents
- module Database.MySQL.Nem.QueryResults
- module Database.MySQL.Nem.Result
- queryResults :: QueryResults r => MySQLConn -> Query -> IO (InputStream r)
- queryStmtResults :: QueryResults r => MySQLConn -> StmtID -> [MySQLValue] -> IO (InputStream r)
Documentation
module Database.MySQL.Nem.Result
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)
xsNotice 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
ResultErrorexception will be thrown. - Sometimes, the compiler needs our help in specifying types. It
can infer that
namemust be aText, due to our use of theunpackfunction. However, we have to tell it the type ofage, 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.