[cduce-users] cduce ocaml-mysql

Alain Frisch Alain.Frisch at inria.fr
Thu Dec 14 21:52:54 CET 2006


miniserveur at ifrance.com wrote:
> Unfortunaly, i'm not ocaml developer, but i'd like find the solution
> and understand my error.
> 
> My error is :
> 
> let db = Mysql.connect ~host:hote ~database:base ~port:None
> ~password:None ~user:login;;
> 
> The problem is that I don't understand the API
> (http://raevnos.pennmush.org/code/ocaml-mysql/doc/Mysql.html)
> and so, I don't can to connect with the database.

The API says that the function Mysql.connect takes a single argument,
which must be a record of type db. Here's how to call this function from
CDuce:

let db = Mysql.connect {
   dbhost = [ hote ];
   dbname = [ base ];
   dbport = [];
   dbpwd  = [];
   dbuser = [ login ]
}

(an OCaml type "t option" is translated in CDuce to [ T? ] where T is
the translation of t).

You could also want to use the Mysql.defaults record and change some
fields. From CDuce, this would look like:

let db = Mysql.connect
   (Mysql.defaults + { dbhost = [ hote ]; dbuser = [ login ] });;


You could also use the Mysql.quick_connect function. It takes several
optional arguments. CDuce simply ignore labels on OCaml arguments. A
call would thus look like:

let db = Mysql.quick_connect [hote] [base] [] [] [login] [];;

(the last [] corresponds to the argument of type unit).


You also need to rename your source file. A file mysql.cd would produce
a mysql.cmo/.cmi, which would be in conflict with the Mysql library itself.


As a last comment: if the code you're planning to write will heavily
rely on OCaml libraries, you might want to try OCamlDuce instead of CDuce.


  Alain



More information about the Cduce-users mailing list