【问题标题】:Postgres: PreparedStatement incorrectly identifying datatypesPostgres:PreparedStatement 错误地识别数据类型
【发布时间】:2013-02-18 20:42:29
【问题描述】:

我无法正确执行此命令。表的第一列是一个自动递增的整数,所以我希望在第 2 列开始输入数据。当我执行以下操作时:

PREPARE fooplan (text, smallint, smallint, text, date, timestamp with time zone) as 
INSERT INTO "table" VALUES($2, $3, $4, $5, $6, $7);
EXECUTE fooplan('Add New Record', 2, 2, 'User', '1999-Jan-08', '04:05:06');

我收到此错误:

SQL error:

ERROR:  column "category_id" is of type smallint but expression is of type text
LINE 2: insert into "MOP" values($2, $3, $4, $5, $6, $7);
                                     ^
HINT:  You will need to rewrite or cast the expression.
In statement:
prepare fooplan (text, smallint, smallint, text, date, time without time zone) as 
insert into "MOP" values($2, $3, $4, $5, $6, $7);
execute fooplan('Add New Mop', 2, 2, 'User', '1999-Jan-08', '04:05:06');

谁能帮我理解我做错了什么?

【问题讨论】:

    标签: postgresql insert prepared-statement auto-increment


    【解决方案1】:

    如果您只是尝试插入值,但没有指定第一个字段值,那么您需要说明您要插入哪些列。

    我不知道你的列叫什么,所以我只称它们为 column_1、column_2 等。

    prepare fooplan (text, smallint, smallint, text, date, timestamp with time zone)
    INSERT INTO MOP(column_2, column_3, column_4, column_5, column_6, column_7) 
    VALUES ($1, $2, $3, £4, $5, $6);
    

    【讨论】:

      【解决方案2】:

      $后面的数字表示参数顺序。

      INSERT INTO "table" VALUES($1, $2, $3, $4, $5, $6);
      

      除此之外,您还必须为要插入的列命名:

      INSERT INTO "table" (col2, col3, col4, col5, col6, col7) 
      VALUES($1, $2, $3, $4, $5, $6);
      

      【讨论】:

        猜你喜欢
        • 2022-01-18
        • 2020-09-17
        • 2016-10-18
        • 1970-01-01
        • 2017-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-10-18
        相关资源
        最近更新 更多