简体   繁体   English

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

[英]unable to add columns to a existing table using C

The following program tries to insert a new row and a new column to an already existing database with a table "people" containing four cols (id, lastname, firstname, phonenumber). 下面的程序尝试将新行和新列插入具有包含四个列(id,lastname,firstname,phonenumber)的“ people”表的现有数据库。 While the row gets inserted successfully, the column is not getting added. 成功插入行后,不会添加列。

    #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;
    }

The program after being compiled and linked gives the following output: 编译并链接后的程序给出以下输出:

    $ ./test
    Successfully inserted value in Table..... 
    We received 0 records.
    ==========================
    ==========================

In the postgresql environment, the table "people" is updated with an extra row and a column containing "TRUE". 在postgresql环境中,表“ people”使用额外的行和包含“ TRUE”的列进行更新。

This is my first program with C embedded with postgresql. 这是我的第一个将C嵌入到postgresql中的程序。 Please help!! 请帮忙!!

This line: 这行:

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

should be: 应该:

 res = PQexec(conn, "UPDATE people SET comment = 'TRUE'");

The EXEC syntax is part of embedded SQL in C with ECPG . EXEC语法是带有ECPG的 C中嵌入式SQL的一部分。 It's not to be combined with PQexec from the C libpq library , which is clearly what you're using given the rest of the source code. 请勿将其与C libpq库中的 PQexec结合使用,在给定其余源代码的情况下,这显然是您正在使用的内容。

Also the result of an UPDATE with no RETURNING clause has PQresultStatus(res)==PGRES_COMMAND_OK , not PGRES_TUPLES_OK 另外,没有PQresultStatus(res)==PGRES_COMMAND_OK子句的UPDATE结果也具有PQresultStatus(res)==PGRES_COMMAND_OK ,而不是PGRES_TUPLES_OK

PGRES_COMMAND_OK PGRES_COMMAND_OK

 Successful completion of a command returning no data. 

See Command Execution Functions 请参阅命令执行功能

You also want to test any PGresult returned by PQexec , not only the result of your last query, because any query may fail. 您还想测试PQexec返回的所有PGresult ,不仅要测试上一个查询的结果,因为任何查询都可能失败。

How can you retrieve data without doing a SELECT statement ? 如何在不执行SELECT语句的情况下检索数据?

Never used PQexec, but i guess the code to add might be like this: 从未使用过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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM