Sep 24, 2010

MySQL access form a Haskell script

This script will connect to a mysql database via haskell script.
Firs of all you must need to download some modules which used to communicate with the database. For that you may you can use
hackage.haskell.org/package/
This link.

You should download and install,
HDBC module and
HDBC-mysql module.

If you encount some problen while you are installing those modules, you should check the dependenci packages as well. You should install those thing also inorder to run those packages without errors. Dependence details are in the package webpage.

Then you can create a test database to test the script. So for that do these thing in MySQL.

>CREATE DATABASE animal
>CREATE TABLE `animal` (`name` char(40) DEFAULT NULL,`category` char(40) DEFAULT NULL)
>INSERT INTO `animal` VALUES ('snake','reptile'), ('crockadile','amphibian')

Now you should create the database.

import Database.HDBC
import Database.HDBC.MySQL

run_quary = do
conn <- connectMySQL defaultMySQLConnectInfo { mysqlHost = "localhost", mysqlDatabase = "animal", mysqlUser = "username",mysqlPassword = "", mysqlUnixSocket="/var/lib/mysql/mysql.sock"}
quickQuery' conn "INSERT INTO animal VALUES ('tuna','fish'),('racoon','mammal')" []


In this script you must change the user name, password and the mysqlUnixSocket path. The user name and its passwd should be able to access the animal database. You can get the mysql.sock path from the file /etc/my.cnf

No comments:

Post a Comment

Your comments are always welcome ...