andrew_j
andrew_j

Reputation: 41

Error while connecting to mysql database, haskell

I'm absolutely newbie to haskell, but I need to write a simple application to work with DB. I'm reading realworldhaskell book, chapter about using databases: http://book.realworldhaskell.org/read/using-databases.html. I've installed HDBC and HDBC-mysql and trying to run:

ghci> :module Database.HDBC Database.HDBC.MySQL

but receive error

attempting to use module ‘Database.HDBC.MySQL’ (./Database/HDBC/MySQL.hs) which is not loaded.

Does someone have some idea how to fix it and by what is is caused? Thanks!

Upvotes: 1

Views: 283

Answers (1)

prayagupadhyay
prayagupadhyay

Reputation: 31232

I could solve the problem installing mysql on MacOS,

brew install mysql
mysql.server start ## mysqld

Followed by

cabal install HDBC
cabal install HDBC-mysql

Then could I create MySQL connection,

import Control.Monad
import Database.HDBC
import Database.HDBC.MySQL

main = do conn <- connectMySQL MySQLConnectInfo { 
                    mysqlHost = "localhost", 
                    mysqlUser = "root", 
                    mysqlPassword = "", 
                    mysqlDatabase = "chat_server", 
                    mysqlPort = 3306, 
                    mysqlUnixSocket = "/tmp/mysql.sock", 
                    mysqlGroup = Just "test"
                  }

          rows <- quickQuery' conn "SELECT 1 + 1" []
          forM_ rows $ \row -> putStrLn $ show row

Note: you might need to update mysqlUnixSocket based on where is it located which can be found with following command:

sudo find / -type s 

Upvotes: 0

Related Questions