[英]Postgresql is not being found within my vala project
所以我有一個項目,但它使用的是 Vala,很難在社區中找到很多東西,但要通過它。 我有這個項目,可能很簡單,但我發現它現在拖了很長時間。
如何連接到 Postgresql?
我在網上運行了示例代碼:
using GLib;
using Postgres;
public static int main (string[] args)
{
string conninfo;
if (args.length > 1) {
conninfo = args[1];
} else {
conninfo = "dbname = postgres";
}
/* Make a connection to the database */
Database conn = Postgres.connect_db (conninfo);
/* Check to see that the backend connection was successfully made */
if (conn.get_status () != ConnectionStatus.OK) {
stderr.printf ("Connection to database failed: %s", conn.get_error_message ());
return 1;
}
/*
* Our test case here involves using a cursor, for which we must be
inside
* a transaction block. We could do the whole thing with a single
* PQexec() of "select * from pg_database", but that's too trivial to
make
* a good example.
*/
/* Start a transaction block */
Result res = conn.exec ("BEGIN");
if (res.get_status () != ExecStatus.COMMAND_OK) {
stderr.printf ("BEGIN command failed: %s",
conn.get_error_message ());
return 1;
}
/*
* Fetch rows from pg_database, the system catalog of databases
*/
res = conn.exec ("DECLARE myportal CURSOR FOR select * from
pg_database");
if (res.get_status () != ExecStatus.COMMAND_OK) {
stderr.printf ("DECLARE CURSOR failed: %s",
conn.get_error_message ());
return 1;
}
res = conn.exec ("FETCH ALL in myportal");
if (res.get_status () != ExecStatus.TUPLES_OK) {
stderr.printf ("FETCH ALL failed: %s", conn.get_error_message
());
return 1;
}
/* first, print out the attribute names */
int nFields = res.get_n_fields ();
for (int i = 0; i < nFields; i++) {
stdout.printf ("%-15s", res.get_field_name (i));
}
stdout.printf ("\n\n");
/* next, print out the rows */
for (int i = 0; i < res.get_n_tuples(); i++) {
for (int j = 0; j < nFields; j++) {
stdout.printf ("%-15s", res.get_value (i, j));
}
stdout.printf ("\n");
}
ConnectionOptions opt = Postgres.get_default_options ();
stdout.printf ("label=%s, keyword=%s\n", opt.label, opt.keyword);
stdout.printf ("db=%s, user=%s, passwd=%s, host=%s, port=%s, tty=%s, options=%s\n", conn.get_db (), conn.get_user (), conn.get_passwd (), conn.get_host (), conn.get_port (), conn.get_tty (), conn.get_options ());
/* close the portal ... we don't bother to check for errors ... */
res = conn.exec ("CLOSE myportal");
/* end the transaction */
res = conn.exec ("END");
return 0;
}
但是每當我跑步時
$ valac --pkg libpq test.vala -X -lpq
在 mingw cmd 行中我得到
C:/.../test.vala.c:7:10: fatal error: postgresql/libpq-fe.h: No such file or directory
7 | #include <postgresql/libpq-fe.h>
| ^~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
Compilation failed: 1 error(s), 0 warning(s)
error: cc exited with status 1
我一直在嘗試很多方法來使用pacman -S
安裝 postgres,包括
pacman -S mingw-w64-x86_64-postgresql
和許多其他版本的 libpq
該錯誤來自 C 編譯器 ( cc
),它說它找不到使用的 C 頭文件。 首先要嘗試的是安裝頭文件,對於 Arch Linux(使用pacman
),我相信libpq-fe.h
頭文件在postgresql-devel
包中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.