Postgres:PreparedStatement错误地识别数据类型

时间:2013-03-04 17:07:16

标签: postgresql insert prepared-statement auto-increment

我无法正确执行此命令。该表的第一列是一个自动递增的整数,所以我希望在第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');

任何人都可以帮我理解我做错了吗?

2 个答案:

答案 0 :(得分:1)

$后的数字是指参数顺序。

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);

答案 1 :(得分: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);
相关问题