4

我在笔记本电脑上使用 postgresql 服务器,并尝试使用 epcg 通过我的 C 程序连接数据库。我编写了以下命令来预编译、编译和运行我的.pgc程序。

PRE-COMPIILE - epcg sql.pgc  
COMPILE -   gcc -c sql.c -I/usr/include/postgresql   
RUN -  gcc -o sql sql.o -L/usr/lib -lecpg

我的程序正在编译并成功运行,即它没有显示任何错误。
但是,当我尝试在命令行提示符下检索我的数据库时,使用命令
COMMAND - psqldatabase

表中没有更新,即我在程序中编写的命令没有在数据库中更新。

以下是.pgc文件中的代码:

#include<stdio.h>


int main()
{


    EXEC SQL CONNECT TO database;

    EXEC SQL create table player(player_id int,player_name varchar(255),team varchar(10));
    EXEC SQL create table player1(player_id int,player_name varchar(255),team varchar(10));
    EXEC SQL INSERT INTO player VALUES(1,'ram','a');

    EXEC SQL COMMIT;

    EXEC SQL DISCONNECT database;

    return 0;
}  

以下是预编译后的 C 代码:

/* Processed by ecpg (4.7.0) */
/* These include files are added by the preprocessor */
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
/* End of automatic include section */

#line 1 "sql.pgc"
#include<stdio.h>

int main()
{
    { ECPGconnect(__LINE__, 0, "vidisha@localhost:5432" , NULL, NULL , NULL, 0); }
#line 8 "sql.pgc"
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table player ( player_id int , player_name varchar ( 255 ) , team varchar ( 10 ) )", ECPGt_EOIT, ECPGt_EORT);}
#line 10 "sql.pgc"
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table player1 ( player_id int , player_name varchar ( 255 ) , team varchar ( 10 ) )", ECPGt_EOIT, ECPGt_EORT);}
#line 11 "sql.pgc"
    { ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into player values ( 1 , 'ram' , 'a' )", ECPGt_EOIT, ECPGt_EORT);}
#line 12 "sql.pgc"
    { ECPGtrans(__LINE__, NULL, "commit");}
#line 14 "sql.pgc"
    { ECPGdisconnect(__LINE__, "vidisha");}
#line 16 "sql.pgc"
    return 0;
}
4

3 回答 3

1

使用您编写的脚本,我的测试数据库也没有任何变化。

因此,使用@vector 建议,如果我提供了我的测试数据库的用户名,一切正常:

使用这个makefile

default:
    ecpg sql.pgc
    gcc -c sql.c -I/usr/include/postgresql
    gcc -o sql sql.o -L/usr/lib -lecpg

而且,作为一个例子,我将使用:

  • testdb作为数据库名称
  • 9876作为港口
  • testuser作为用户名
  • testpass作为密码

有了这个sql.pgc

#include <stdio.h>

int main() {
    EXEC SQL CONNECT TO testdb@localhost:9876 USER 'testuser' IDENTIFIED BY 'testpass';

    EXEC SQL create table player(player_id int,player_name varchar(255),team varchar(10));
    EXEC SQL create table player1(player_id int,player_name varchar(255),team varchar(10));
    EXEC SQL INSERT INTO player VALUES(1,'ram','a');

    EXEC SQL COMMIT;

    EXEC SQL DISCONNECT;

    return 0;
}

一切运作良好。

于 2012-09-04T15:52:16.123 回答
1

You also have to provide the password and the default port is 5432. This code works fine for me. testdb- databaseName, postgres - username, 123321 - password

_Also after once executing this program you have to remove both the create table lines else your code will not work (as the tables are alredy created). You will get no error as you have not done error handling. To learn about error handling and more click here. _

#include<stdio.h>


int main()
{


    EXEC SQL CONNECT TO testdb@localhost:5432 USER 'postgres' USING '123321';

    EXEC SQL create table player(player_id int,player_name varchar(255),team varchar(10));
    EXEC SQL create table player1(player_id int,player_name varchar(255),team varchar(10));
    EXEC SQL INSERT INTO player VALUES(1,'ram','a');

    EXEC SQL COMMIT;

    EXEC SQL DISCONNECT database;

    return 0;
}
于 2012-09-04T19:04:16.560 回答
0

您必须将搜索路径设置为当前数据库。

EXEC SQL SET SEARCH_PATH TO "your database";

另外,最后不要忘记提交。

EXEC SQL COMMIT;
于 2017-04-14T15:45:39.547 回答