无法使用PG';连接PostgreSQL数据库;奥卡姆
我最近尝试使用OCaml处理Postgres数据库。我找到了PG'Ocaml库,并研究了第一个示例。这就是错误:无法使用PG';连接PostgreSQL数据库;奥卡姆,postgresql,ocaml,Postgresql,Ocaml,我最近尝试使用OCaml处理Postgres数据库。我找到了PG'Ocaml库,并研究了第一个示例。这就是错误: $ make ocamlfind ocamlc -package pgocaml,pgocaml.syntax -syntax camlp4o -c test.ml File "test.ml", line 7, characters 23-146 (end at line 12, character 2): Camlp4: Uncaught exception: Unix.Unix
$ make
ocamlfind ocamlc -package pgocaml,pgocaml.syntax -syntax camlp4o -c test.ml
File "test.ml", line 7, characters 23-146 (end at line 12, character 2):
Camlp4: Uncaught exception: Unix.Unix_error (20 | CstTag21, "connect", "")
File "test.ml", line 1:
Error: Preprocessor error
make: *** [test.cmo] Error 2
这里有一个类似的问题(),但结论对我来说并不适用,因为日志记录使用
$ psql -h localhost -d adam - U adam
完全有效,我已经包括了线
let dbh = PGOCaml.connect ~host:"localhost" ~user:"adam" ~datebase:"adam"
~password:"******" () in
输入我的代码
这是我的Makefile:
PROJECT := test
LINK_PKG := pgocaml
COMP_PKG := pgocaml,pgocaml.syntax
all: $(PROJECT)
$(PROJECT): $(PROJECT).cmo
ocamlfind ocamlc -package $(LINK_PKG) -linkpkg -o $@ $<
$(PROJECT).cmo: $(PROJECT).ml
ocamlfind ocamlc -package $(COMP_PKG) -syntax camlp4o -c $<
错误来自camlp4和第7行,第一行使用PG'OCaml的语法扩展。因此,这可能是一个解析问题,与连接到数据库无关。尝试简化您的示例。您是对的,语法PGSQL(dbh)“…”是实际问题。谢谢!
open Printf
let () =
let dbh = PGOCaml.connect ~host:"localhost" ~user:"adam" ~datebase:"adam"
~password:"******" () in
PGSQL(dbh) "execute" "create temporary table employees (
id serial not null primary key,
name text not null,
salary int4 not null,
email text
)";
let insert =
PGPREPARE(dbh) "opt-args"
"insert into employees (name, salary, email)
values ($name, $salary, $?email)"
in
insert "Ann" 10_000_l ();
insert "Bob" 45_000_l ();
insert "Jim" 20_000_l ();
insert ~name:"Mary" ~salary:30_000_l ~email:"mary@example.com" ();
let rows = PGSQL(dbh) "select id, name, salary, email from employees" in
List.iter (
fun (id, name, salary, email) ->
let email = match email with Some email -> email | None -> "-" in
printf "%ld %S %ld %S\n" id name salary email
) rows;
let ids = [ 1_l; 3_l ] in
let rows = PGSQL(dbh) "select id, name, salary, email from employees
where id in $@ids" in
List.iter (
fun (id, name, salary, email) ->
let email = match email with Some email -> email | None -> "-" in
printf "%ld %S %ld %S\n" id name salary email
) rows;
PGOCaml.close dbh