I'm trying to use Pgocaml for database interactions within my application.
This is the file I'm trying to compile:
let () =
let dbh = PGOCaml.connect () in
let insert name salary email =
PGSQL(dbh) "insert into employees (name, salary, email) values ($name, $salary, $?email)"
in
insert "Ann" 10_000_l None;
insert "Bob" 45_000_l None;
insert "Jim" 20_000_l None;
insert "Mary" 30_000_l (Some "mary@example.com");
let print_row (id, name, salary, email) =
let email = match email with Some email -> email | None -> " -"
in Printf.printf "%ld %S %ld %S\n" id name salary email in
let rows =
PGSQL(dbh) "select id, name, salary, email from employees"
in List.iter print_row rows;
PGOCaml.close dbh
This is how I am trying to compile it:
ocamlbuild -use-ocamlfind -pkg pgocaml pgex.native
and this is the error I am getting:
+ ocamlfind ocamldep -package pgocaml -modules pgex.ml > pgex.ml.depends
File "pgex.ml", line 4, characters 19-97:
Error: Syntax error
Command exited with code 2.
Why am I getting this error?
Thanks in advance!
PG'OCaml is a syntax extension, it is not a regular ocaml code, so you need to take extra steps to let it perform its magic. First, read the tutorial which also explains how to compile projects with pgocaml. Second, tell the build system that pgex.ml
should be preprocessed with camlp4
- i.e. create _tags
file with the contents <pgex.ml>: syntax(camlp4o)
.