请教:有关linux下c写入postgre的问题,谢谢!
代码如下:
/*
* testlibpq.c
* Test the C version of LIBPQ, the POSTGRES frontend library.
*
*
*/
#include <stdio.h>
#include "libpq-fe.h"
static void
exit_nicely(PGconn *conn)
{
PQfinish(conn);
exit(1);
}
int
main()
{
char *pghost,*pgport,*pgoptions,*pgtty;
char *dbName;
char str[128],name[15],gender[15],telephone[15],phone[15],addres
s[15];
int nFields;
int i,j,YorN;
PGconn *conn;
PGresult *res;
pghost = NULL;
pgport = NULL;
pgoptions = NULL;
pgtty = NULL;
dbName = "contaction";
conn = PQsetdb(pghost,pgport,pgoptions,pgtty,dbName);
if (PQstatus(conn) == CONNECTION_BAD)
{
fprintf(stderr, "Connection to database '%s' failed.\n", dbName);
fprintf(stderr, "%s", PQerrorMessage(conn));
exit_nicely(conn);
}
printf("Do you want to insert ?(y/n)\n");
YorN=getchar();
if (YorN=='y')
{
printf("please input the name,gender,telephone,phone and address\n");
scanf(stdin,"%s %s %s %s %s",name,gender,telephone,phone,address);
sprintf(str,"insert into gy61 values(name,gender,telephone,phone,address);");
res=PQexec(conn,str);
if(res==NULL)
printf("%s","insert record error");
}
以下代码省略
用make编译通过
执行是输入 rt
female
139517535
359
nanjing
出现:ERROR:Attribute 'name' not found
根本没有写入数据库
请教高手如何解决。
注:我数据库中name,gender,telephone,phone,address的类型全是char[15]
数据插入语句:insert into gy61 values('rt','female','13951753','359','nanjing'
);
:confused: