Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 无法使用C将列添加到现有表中_Sql_C_Postgresql - Fatal编程技术网

Sql 无法使用C将列添加到现有表中

Sql 无法使用C将列添加到现有表中,sql,c,postgresql,Sql,C,Postgresql,下面的程序尝试将新行和新列插入到已存在的数据库中,该数据库的表people包含四个cols id、lastname、firstname和phonenumber。成功插入行时,未添加列 #include <stdio.h> #include <stdlib.h> #include <libpq-fe.h> #include <string.h> int main() { PGconn *c

下面的程序尝试将新行和新列插入到已存在的数据库中,该数据库的表people包含四个cols id、lastname、firstname和phonenumber。成功插入行时,未添加列

    #include <stdio.h>
    #include <stdlib.h>
    #include <libpq-fe.h>
    #include <string.h>

    int main() 
    {
     PGconn *conn;
     PGresult *res;
     int rec_count;
     int row;
     int col;

     conn = PQconnectdb("dbname=test host=localhost user=abc1 password=xyz1");

         if(PQstatus(conn) == CONNECTION_BAD) {
             puts("We were unable to connect to the database");
             exit(0);
         }

     res = PQexec(conn,"INSERT INTO people VALUES (5, 'XXX', 'YYY', '7633839276');");
     if(PQresultStatus(res) != PGRES_COMMAND_OK) {
        fprintf(stderr, "Insertion Failed1: %s", PQerrorMessage(conn));
        PQclear(res);
         }
     else
        printf("Successfully inserted value in Table..... \n");

         res = PQexec(conn,"update people set phonenumber=\'5055559999\' where id=3");
     if(PQresultStatus(res) != PGRES_COMMAND_OK) {
        fprintf(stderr, "Insertion Failed2: %s", PQerrorMessage(conn));
        PQclear(res);
         }

     res = PQexec(conn, "ALTER TABLE people ADD comment VARCHAR(100) DEFAULT 'TRUE'");
     if(PQresultStatus(res) != PGRES_COMMAND_OK) {
        fprintf(stderr, "Insertion Failed3: %s", PQerrorMessage(conn));
        PQclear(res);
         }

  rec_count = PQntuples(res);

      printf("We received %d records.\n", rec_count);
      puts("==========================");

      for(row=0; row<rec_count; row++) {
          for(col=0; col<3; col++) {
              printf("%s\t", PQgetvalue(res, row, col));
           }
           puts("");
        }

      puts("==========================");

      PQclear(res);

      PQfinish(conn);

      return 0;
    }
在postgresql环境中,表people会更新为一个额外的行和一个包含TRUE的列

这是我第一个用C嵌入postgresql的程序。请帮忙

这一行:

 res = PQexec(conn, "EXEC('UPDATE people SET comment = ''TRUE'';');");
应该是:

 res = PQexec(conn, "UPDATE people SET comment = 'TRUE'");
EXEC语法是C中嵌入式SQL的一部分。它不能与来自的PQexec组合,这显然是您在给定其余源代码的情况下使用的

另外,不返回子句的更新的结果具有PQresultStatusres==PGRES\u COMMAND\u OK,而不是PGRES\u TUPLES\u OK

PGRES_命令_正常

Successful completion of a command returning no data.


您还需要测试PQexec返回的任何PGresult,而不仅仅是上一次查询的结果,因为任何查询都可能失败。

如果不执行SELECT语句,如何检索数据

从未使用过PQexec,但我猜要添加的代码可能如下所示:

 res = PQexec(conn, "ALTER TABLE people ADD comment VARCHAR(100) DEFAULT 'TRUE'");
 if(PQresultStatus(res) != PGRES_COMMAND_OK) {
    fprintf(stderr, "Insertion Failed3: %s", PQerrorMessage(conn));
    PQclear(res);
 }

//Add PQexec and select statement here.
//Something "like" this.
 res = PQexec(conn, "SELECT * FROM people;");
 if(PQresultStatus(res) != PGRES_TUPLES_OK) {
    fprintf(stderr, "Select Failed: %s", PQerrorMessage(conn));
    PQclear(res);
 }

  rec_count = PQntuples(res);

  printf("We received %d records.\n", rec_count);

抱歉在不知道其确切用法的情况下放入EXEC。我最初是按照你的建议做的,但那不起作用。谷歌搜索告诉我这个EXEC函数,我想试试运气。但是,没有什么是仍然有效的!!始终添加行,但不添加列注释。我还相应地更改了PQresultStatusres行。@user3258515:有关INSERT的错误检查和显示类型,需要对ALTER TABLE语句进行相同的更改。事实上,对于每次调用PQexec,在每个PQexec步骤中进行的错误检查都显示用户没有权限,因此在更正后,我可以插入列,但只能在postgresql环境中插入。然而,程序不会给出输出。它给出以下输出:在表中成功插入值。。。。。我们收到了0条记录。我们没有得到任何数据!这意味着rec_count=pqn次;它不起作用。你能在这里提出一个可能的错误吗?这是有道理的。更新不检索任何行。上面的SELECT应该检索行,但您当前的代码不会尝试读取它们。我只是编辑了工作代码。向表中添加col后,我不会更新该表。它在Postgresql环境中工作,但在程序中不工作。请提供进一步建议。按照Daniel的建议编辑发布工作代码,添加行和列,但仅在Postgresql环境中,而不在程序中。我希望你昨天回复。。。我一直在努力,最终以你上面所说的方式解决了这个问题。。所以感谢你和丹尼尔,他们一直在推动我。。希望有两个答案的投票选择!!
 res = PQexec(conn, "ALTER TABLE people ADD comment VARCHAR(100) DEFAULT 'TRUE'");
 if(PQresultStatus(res) != PGRES_COMMAND_OK) {
    fprintf(stderr, "Insertion Failed3: %s", PQerrorMessage(conn));
    PQclear(res);
 }

//Add PQexec and select statement here.
//Something "like" this.
 res = PQexec(conn, "SELECT * FROM people;");
 if(PQresultStatus(res) != PGRES_TUPLES_OK) {
    fprintf(stderr, "Select Failed: %s", PQerrorMessage(conn));
    PQclear(res);
 }

  rec_count = PQntuples(res);

  printf("We received %d records.\n", rec_count);