为什么我在这个简单的 INSERT 查询中遇到错误?

问题描述 投票:0回答:1

在 Win 10 上使用 Postgres 14。运行这个简单的查询:

mydata=# SELECT * FROM contact_test;
 cntct_id | fname | lname | email | street | unit | town | state | post_code
----------+-------+-------+-------+--------+------+------+-------+-----------
(0 rows)


mydata=# INSERT INTO contact_test (fname, lname, email) VALUES ('Robert', 'Johnson', '[email protected]');
ERROR:  malformed array literal: "Robert"
LINE 1: ...T INTO contact_test (fname, lname, email) VALUES ('Robert', ...
                                                             ^
DETAIL:  Array value must start with "{" or dimension information.

我在这里查找语法:

样本与我的陈述基本相同:

INSERT INTO products (product_no, name, price) VALUES
(1, 'Cheese', 9.99);

我错过了什么?

postgresql sql-insert
1个回答
2
投票

鉴于您在评论中添加的表定义,此语句将起作用:

INSERT INTO contact_test (fname, lname, email) VALUES ('{R}', '{J}', 'r');

fname
lname
array 类型,因此您需要传递 array literals(或 array constructors)。例子:

但是你的表定义当然是胡说八道。您不需要内部枚举类型

"char"
,它包含一个 ASCII 字母。你想要
varchar
text
。而且我也不相信你想要数组类型(
"char"[]
是一个
"char"
的数组)。
相应地修复您的表定义!喜欢:

CREATE TABLE contact_test (
  cntct_id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY
, fname text NOT NULL
, lname text NOT NULL
, email text NOT NULL
);

然后你原来的

INSERT
声明有效。

参见:

© www.soinside.com 2019 - 2024. All rights reserved.