我在我的笔记本电脑上使用
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 – psql数据库
表中没有更新,即我在程序中编写的命令没有在数据库中更新.
#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,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__,"vidisha@localhost:5432",NULL,0); } #line 8 "sql.pgc" { ECPGdo(__LINE__,1,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__,"create table player1 ( player_id int,ECPGt_EORT);} #line 11 "sql.pgc" { ECPGdo(__LINE__,"insert into player values ( 1,'a' )",ECPGt_EORT);} #line 12 "sql.pgc" { ECPGtrans(__LINE__,"commit");} #line 14 "sql.pgc" { ECPGdisconnect(__LINE__,"vidisha");} #line 16 "sql.pgc" return 0; }
解决方法
使用您编写的脚本,我的测试数据库也没有任何变化.
所以,使用@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,'a'); EXEC sql COMMIT; EXEC sql DISCONNECT; return 0; }
一切顺利.