简体   繁体   English

如何在postgresql中向表中插入值

[英]how to insert values to table in postgresql

When i try to insert values to my table, it doesn't work and i get this error:当我尝试向我的表中插入值时,它不起作用并且我收到此错误:

ERROR:  invalid input syntax for integer: "regular"
LINE 2: ('100', 'Astronomy Today', '1st publisher', 6, 'regular', 4/...
                                                                   ^

This is my code:这是我的代码:

CREATE TABLE Subscribes
(cid numeric(5,0),
title varchar (30),
publisher varchar(30),
period integer, 
offer varchar (10), 
sfrom date,
primary key(cid, title, publisher, offer, period),
foreign key(cid) references Customer(cid),
foreign key(title, publisher,offer, period) references Pricing(title, 
publisher,offer, period));


INSERT INTO Subscribes VALUES
('100', 'Astronomy Today', '1st publisher', 6, 'regular', 4/4/2015),
('100', 'Bridal Guide', '3rd publisher', 12, 'regular', 1/5/2015),
('100', 'Click Magazine', '3rd publisher', 6, 'regular', 20/12/2014),
('107', 'Bridal Guide', '3rd publisher', 12, 'regular', 29/4/2015);

PS: I've checked every variable and couldn't find the problem PS:我已经检查了每个变量并找不到问题

Place single quotes around date like below在日期周围放置单引号,如下所示

INSERT INTO Subscribes VALUES
('100', 'Astronomy Today', '1st publisher', 6, 'regular', '2015/4/4')

someone was faster than me :)有人比我快:)

 CREATE TABLE Subscribes       (cid numeric(5,0), title varchar (30), publisher,varchar(30), period integer, offer varchar (10), sfrom date ....
 INSERT INTO Subscribes VALUES ('100'           , 'Astronomy Today' , '1st publisher'      , 6           , 'regular'           , 4/4/2015)  ..... 

you have problem inserting date field, its not quoted ... and its format is not well formed.你在插入日期字段时遇到问题,它没有被引用......并且它的格式格式不正确。 you should use standard system format, '2015-04-04' or use to_date(text, text) conversion function from text to date inside insert.您应该使用标准系统格式,'2015-04-04' 或使用 to_date(text, text) 转换函数从插入中的文本到日期。

When using INSERT , you should take care with the code you write:使用INSERT ,您应该注意您编写的代码:

  • Always list the columns explicitly.始终明确列出列。
  • Don't wrap numbers in single quotes.不要将数字括在单引号中。
  • Use the proper format for date constants.为日期常量使用正确的格式。

So, it should look more like this:所以,它应该看起来更像这样:

INSERT INTO Subscribes(cid, title, publisher, period, offer, sfrom)
     VALUES (100, 'Astronomy Today', '1st publisher', 6, 'regular', '2015-04-04'), . . .

To insert a date you should use TO_DATE要插入日期,您应该使用 TO_DATE

The following insert should work for your case:以下插入应该适用于您的情况:

INSERT INTO Subscribes(cid, title, publisher, period, offer, sfrom)
VALUES (100, 'Astronomy Today', '1st publisher', 6, 'regular', TO_DATE('04/04/2015', 'DD/MM/YYYY'))

29/4/2015 is not a valid date literal. 29/4/2015不是有效的日期文字。 Either use a proper ANSI literal: date '2015-04-29' or use the to_date() function (I prefer ANSI literals, because it's less typing)要么使用适当的 ANSI 文字: date '2015-04-29'或使用to_date()函数(我更喜欢 ANSI 文字,因为它的输入较少)

And '100' is not a number it's a character literal. '100'不是数字,而是字符文字。 Number don't need single quotes.数字不需要单引号。 Use 100 instead.改用100

You should also always qualify the column names to make the statement more robust:您还应该始终限定列名以使语句更健壮:

INSERT INTO Subscribes  (cid, title, publisher, period, offer, sfrom)

Putting all together this results in something like this:把所有这些放在一起,结果是这样的:

INSERT INTO Subscribes 
  (cid, title, publisher, period, offer, sfrom)
VALUES 
(100, 'Astronomy Today', '1st publisher', 6, 'regular', date '2015-04-04'),
(100, 'Bridal Guide', '3rd publisher', 12, 'regular', date '2015-05-01'),
(100, 'Click Magazine', '3rd publisher', 6, 'regular', date '2014-12-20'),
(107, 'Bridal Guide', '3rd publisher', 12, 'regular', date '2015-04-29');

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

相关问题 PostgreSQL:如何将数组中的值插入表中 - PostgreSQL:How to insert values in an array into a table 如何将值插入PostgreSQL中新添加的列的行中 - How to insert values into the rows of a newly added column of a table in PostgreSQL 在值中带有撇号的 PostgreSQL 表中插入 ARRAY - Insert ARRAY in PostgreSQL table with apostrophes in values 在插入table / PostgreSQL之前更改值 - Change values before insert into table / PostgreSQL 在PostgreSQL中插入表A期间如何有条件地在表B中插入记录? - How to conditionally insert a record in table B during insert into table A in PostgreSQL? 如何在 Postgresql 中的列值等于时从另一个表中插入数据? - How to insert data from another table while column values equals in Postgresql? 如何在postgresql中没有公共字段的情况下从另一个表插入一列及其值? - How to insert a column and it's values from another table without a common field in postgresql? PostgreSQL:如何在 INSERT 查询中强制设置值? - PostgreSQL: How to make values obligatory in INSERT querys? PostgreSQL:如何通过循环将数据插入数据库表? - PostgreSQL: How to insert data into database table with loop? 如何在PostgreSQL数据库表中插入其他字符? - How to insert additional characters into a PostgreSQL Database Table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM