HDBC-Example: Difference between revisions
(minimalist example for HDBC) |
mNo edit summary |
||
Line 3: | Line 3: | ||
For this example I assume that you have a working installation of PostgreSQL and HDBC. Further I assume the existance of a database 'testdb' and a user 'testuser'. | For this example I assume that you have a working installation of PostgreSQL and HDBC. Further I assume the existance of a database 'testdb' and a user 'testuser'. | ||
We need some imports. Exception is optional, but since stuff can go wrong... | |||
<haskell> | <haskell> | ||
>import Control.Exception | >import Control.Exception | ||
>import Database.HDBC | >import Database.HDBC | ||
Line 11: | Line 11: | ||
>main | >main | ||
</haskell> | |||
We catch Exceptions with catchDyn and handle them with our handler function below | We catch Exceptions with catchDyn and handle them with our handler function below | ||
<haskell> | |||
> catchDyn (do | > catchDyn (do | ||
</haskell> | |||
Step 1 is opening a connection to your Database server. | Step 1 is opening a connection to your Database server. | ||
<haskell> | |||
> c <- connectPostgreSQL "host=localhost dbname=testdb user=testuser password=pass" | > c <- connectPostgreSQL "host=localhost dbname=testdb user=testuser password=pass" | ||
</haskell> | |||
SQL statements are prepared first. Questionmarks are later replaced | SQL statements are prepared first. Questionmarks are later replaced | ||
with parameters. | with parameters. | ||
<haskell> | |||
> state <- prepare c "INSERT INTO testtable values (?,?);" | > state <- prepare c "INSERT INTO testtable values (?,?);" | ||
> select <- prepare c "SELECT * FROM testtable;" | > select <- prepare c "SELECT * FROM testtable;" | ||
</haskell> | |||
to actually execute a statement, use execute. The list of parameters | to actually execute a statement, use execute. The list of parameters | ||
replaces the ? in the statement | replaces the ? in the statement | ||
<haskell> | |||
> execute state [toSql "muhmuh", toSql (40::Int)] | > execute state [toSql "muhmuh", toSql (40::Int)] | ||
> execute select [] | > execute select [] | ||
</haskell> | |||
there are various ways to get results from an executed statement. | there are various ways to get results from an executed statement. | ||
We use the lazy fetchAllRows, which works like hGetContents. Refer | We use the lazy fetchAllRows, which works like hGetContents. Refer | ||
to the API for other functions. Note that the statement magically | to the API for other functions. Note that the statement magically | ||
remembers it's been executed and carries the results around | remembers it's been executed and carries the results around | ||
<haskell> | |||
> result <- fetchAllRows select | > result <- fetchAllRows select | ||
> putStrLn $ show result | > putStrLn $ show result | ||
</haskell> | |||
In HDBC everything runs inside a transaction. That means no changes | In HDBC everything runs inside a transaction. That means no changes | ||
are actually saved until you call commit. You can rollback to the | are actually saved until you call commit. You can rollback to the | ||
last commit. | last commit. | ||
<haskell> | |||
> commit c | > commit c | ||
</haskell> | |||
Finally, disconnect from the database | Finally, disconnect from the database | ||
<haskell> | |||
> disconnect c | > disconnect c | ||
> return () | > return () | ||
> ) | > ) | ||
</haskell> | |||
The handler merely prints the error message. In a real program you | The handler merely prints the error message. In a real program you | ||
would to do some cleanup here. | would to do some cleanup here. | ||
<haskell> | |||
> handler where | > handler where | ||
> handler :: SqlError -> IO () | > handler :: SqlError -> IO () | ||
> handler err = print $ "Oh no: " ++ show err | > handler err = print $ "Oh no: " ++ show err | ||
</haskell> | </haskell> |
Revision as of 08:59, 3 March 2008
HDBC Example
For this example I assume that you have a working installation of PostgreSQL and HDBC. Further I assume the existance of a database 'testdb' and a user 'testuser'.
We need some imports. Exception is optional, but since stuff can go wrong...
>import Control.Exception
>import Database.HDBC
>import Database.HDBC.PostgreSQL (connectPostgreSQL)
>main
We catch Exceptions with catchDyn and handle them with our handler function below
> catchDyn (do
Step 1 is opening a connection to your Database server.
> c <- connectPostgreSQL "host=localhost dbname=testdb user=testuser password=pass"
SQL statements are prepared first. Questionmarks are later replaced with parameters.
> state <- prepare c "INSERT INTO testtable values (?,?);"
> select <- prepare c "SELECT * FROM testtable;"
to actually execute a statement, use execute. The list of parameters replaces the ? in the statement
> execute state [toSql "muhmuh", toSql (40::Int)]
> execute select []
there are various ways to get results from an executed statement. We use the lazy fetchAllRows, which works like hGetContents. Refer to the API for other functions. Note that the statement magically remembers it's been executed and carries the results around
> result <- fetchAllRows select
> putStrLn $ show result
In HDBC everything runs inside a transaction. That means no changes are actually saved until you call commit. You can rollback to the last commit.
> commit c
Finally, disconnect from the database
> disconnect c
> return ()
> )
The handler merely prints the error message. In a real program you would to do some cleanup here.
> handler where
> handler :: SqlError -> IO ()
> handler err = print $ "Oh no: " ++ show err