database 将日期插入 PostgreSQL:来自和至的列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42767733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:04:34  来源:igfitidea点击:

Inserting date into PostgreSQL: Columns from and to

databasepostgresql

提问by Aleix Alcover

I can't insert a dateinto a PostgreSQL 9.6 table.

我无法将 a 插入date到 PostgreSQL 9.6 表中。

This is the INSERT Query:

这是插入查询:

INSERT INTO rates (idproperty, from, to, price)
VALUES (1, '2017-03-14', '2017-04-30', 130);

And that's the result:

这就是结果:

ERROR:  syntax error at or near "from"
LINE 1: INSERT INTO rates (idproperty, from, to, price)

The columns fromand toare defined with the data type date.

fromto用数据类型定义date

Thanks

谢谢

回答by user3402754

I believe you have used postgresql reserved words - fromand toto create your table. In order to use them in your query, they need to be enclosed in quotes "

我相信您已经使用了 postgresql 保留字 -fromto创建了您的表。为了在您的查询中使用它们,它们需要用引号引起来"

Try it this way:

试试这个方法:

INSERT INTO rates (idproperty, "from", "to", price)
VALUES (1, '2017-03-14', '2017-04-30', 130);

回答by Gab

fromand toare reserved words in PostgreSQL. You need to escape them with ":

fromto是 PostgreSQL 中的保留字。您需要使用以下命令来逃避它们"

-- This fails:

test=# SELECT from FROM rates;
ERROR:  syntax error at or near "FROM"
LINE 1: SELECT from FROM rates;
                    ^

-- This works:

test=# SELECT "from" FROM rates;
 from 
------
(0 rows)

See list of reserved words: https://www.postgresql.org/docs/current/static/sql-keywords-appendix.html

查看保留字列表:https: //www.postgresql.org/docs/current/static/sql-keywords-appendix.html

回答by EAK TEAM

from and to is not a correct field it is reserved , please change to from_i for example and to_i

from 和 to 不是一个正确的字段,它是保留的,例如请更改为 from_i 和 to_i