1

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!

andrew_j
  • 41
  • 7
  • Did you install [HDBC-sqlite3](https://hackage.haskell.org/package/HDBC-sqlite3), which is the one exporting the Database.HDBC.Sqlite3 module? I see you're trying to import that module but mentioned installing HDBC-mysql instead. – jpvillaisaza Oct 24 '16 at 13:05
  • The command must have been `:module Database.HDBC Database.HDBC.MySQL` . Meanwhile, though, you are opening `ghci` or `cabal repl` inside the main directory of the `HDBC-mysql` source, so it is looking to the local file `./Database/HDBC/MySQL.hs`, as the message says. You should just install `HDBC-mysql` one way or another, and stay out of the source for it. – Michael Oct 24 '16 at 13:19
  • @jpvillaisaza ooops, that's a mistake in my question. of course, not. I'm trying exporting Database.HDBC.MySQL, it could be seen from error, which I receiving – andrew_j Oct 24 '16 at 13:20
  • @Michael please, see my previous comment. I still have a problem – andrew_j Oct 24 '16 at 13:21
  • Right, I saw what you had done. This particular problem entails that you are opening ghci inside the source directory for `HDBC-mysql` - how else would `ghci` *know* that it is near a file called `(./Database/HDBC/MySQL.hs)`. It might make sense to do this if you are developing `HDBC-mysql`, otherwise not. My sense is that you should install `HDBC-mysql` and get the source off of the computer, since it is confusing. – Michael Oct 24 '16 at 13:25
  • 1
    @Michael thanks, I've got it! Everything works! – andrew_j Oct 24 '16 at 13:26

1 Answers1

0

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 
prayagupa
  • 30,204
  • 14
  • 155
  • 192