【问题标题】:pg_copy_from will not let my index column increment itself, and will not take blank value eitherpg_copy_from 不会让我的索引列自己增加,也不会取空值
【发布时间】:2014-11-04 22:41:57
【问题描述】:

我有一个数组,我想用pg_copy_from 填充一个 pgsql 表。但是当我没有为我的序列号index 列提供信息时,我收到以下错误。

ERROR: there is data for a column index

如何强制pg_copy_from 忽略我的索引列,让它独立增加。因为它需要逗号分隔的值,所以我试着在某个时候像这样在索引的位置传递一个空白字符串:

array_walk($logs, function(&$record) { 
    "'', " . $record; 
});

但它没有工作。我还尝试制作一个表格,其中index 是第二列,我没有传递第二个值,但我收到了同样的错误。有这个选项吗?

【问题讨论】:

    标签: php arrays postgresql insert


    【解决方案1】:

    免责声明。我不是 PHP 开发人员,但听起来您可能只需要将单词 DEFAULT 插入到您的“索引”列的数组中(我假设这在您的主键中并且它有一个排序器)。例如:

    create temp table foobar(id serial, foo text);
    insert into foobar values (DEFAULT, 'hello');
    insert into foobar values (DEFAULT, 'world');
    select * from foobar;
    +----+-------+
    | id |  foo  |
    +----+-------+
    |  1 | hello |
    |  2 | world |
    +----+-------+
    (2 rows)
    

    【讨论】:

      【解决方案2】:

      pg_copy_from 不允许您排除某些列。由于您自己的 COPY 语句,pg_query 和 pg_put_line 的组合确实允许这样做:

      <?php 
        $conn = pg_pconnect("dbname=foo");
        pg_query($conn, "create table bar (a serial, b char(16), d float8)"); // just an example
        pg_query($conn, "copy bar(b, c) from stdin"); // only columns b and c !
        pg_put_line($conn, "hello world\t4.5\n");
        pg_put_line($conn, "goodbye world\t7.11\n");
        pg_put_line($conn, "\\.\n");
        pg_end_copy($conn);
      ?>
      

      【讨论】:

      • 谢谢,我会试试这个。我很好奇,为什么会这样:array_walk($logs, function (&amp;$item, $key) { $item = time() . $key . ", " . $item;}); 连接会产生错误`PHP 警告:pg_copy_from():复制命令失败:错误:“条目”列缺少数据
      猜你喜欢
      • 2017-07-09
      • 1970-01-01
      • 2013-05-01
      • 1970-01-01
      • 2019-05-14
      • 1970-01-01
      • 2015-01-11
      • 2013-03-02
      • 2018-04-10
      相关资源
      最近更新 更多