HaskellでMySQLにアクセス
サンドボックスの作成
(root)foo:mysqlsample foo$ cabal sandbox init Writing a default package environment file to /Users/foo/work03/haskelldatabase/mysqlsample/cabal.sandbox.config Creating a new sandbox at /Users/foo/work03/haskelldatabase/mysqlsample/.cabal-sandbox
ライブラリのインストール
(root)foo:mysqlsample foo$ cabal install HDBC-mysql Warning: The package list for 'hackage.haskell.org' is 22 days old. Run 'cabal update' to get the latest list of available packages. Resolving dependencies... Notice: installing into a sandbox located at /Users/foo/work03/haskelldatabase/mysqlsample/.cabal-sandbox Configuring old-locale-1.0.0.7... Configuring mtl-2.2.1... Configuring utf8-string-1.0.1.1... Configuring text-1.2.2.1... Building utf8-string-1.0.1.1... Building old-locale-1.0.0.7... Building mtl-2.2.1... Building text-1.2.2.1... Installed old-locale-1.0.0.7 Configuring old-time-1.1.0.3... Building old-time-1.1.0.3... Installed mtl-2.2.1 Installed utf8-string-1.0.1.1 Installed old-time-1.1.0.3 Installed text-1.2.2.1 Configuring convertible-1.1.1.0... Building convertible-1.1.1.0... Installed convertible-1.1.1.0 Configuring HDBC-2.4.0.1... Building HDBC-2.4.0.1... Installed HDBC-2.4.0.1 Downloading HDBC-mysql-0.7.1.0... Configuring HDBC-mysql-0.7.1.0... Building HDBC-mysql-0.7.1.0... Installed HDBC-mysql-0.7.1.0 Updating documentation index /Users/foo/work03/haskelldatabase/mysqlsample/.cabal-sandbox/share/doc/x86_64-osx-ghc-8.0.1/index.html
インポート
(root)foo:mysqlsample foo$ cabal repl GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help Prelude> import Database.HDBC Prelude Database.HDBC> import Database.HDBC.MySQL
コネクション取得
Prelude Database.HDBC Database.HDBC.MySQL> connectMySQL defaultMySQLConnectInfo {mysqlUser = "redmine", mysqlPassword = "my_password", mysqlDatabase="redmine"} Prelude Database.HDBC Database.HDBC.MySQL> let con = connectMySQL defaultMySQLConnectInfo {mysqlUser = "redmine", mysqlPassword = "my_password", mysqlDatabase="redmine"} Prelude Database.HDBC Database.HDBC.MySQL> :t con con :: IO Connection
select実行
Prelude Database.HDBC Database.HDBC.MySQL> con >>= \x -> prepare x "select * from USERS" >>= (\y -> execute y [] >>= \_ -> fetchRow y) Just [SqlInt32 1,SqlByteString "admin",SqlByteString "a99fe2fb647cb2a5e059e095c93d2ccc0588f76d",SqlByteString "Redmine",SqlByteString "Admin",SqlInt32 1,SqlInt32 1,SqlLocalTime 2015-02-01 20:33:33,SqlByteString "",SqlNull,SqlLocalTime 2015-12-07 20:51:38,SqlLocalTime 2015-12-07 21:39:45,SqlByteString "User",SqlNull,SqlByteString "all",SqlByteString "cb24cc9080698d1eb43f3c8b32189e4e",SqlInt32 0,SqlLocalTime 2015-12-07 21:39:45]