PostgreSQL 插入查询

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8070169/
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-10 23:11:28  来源:igfitidea点击:

PostgreSQL insert query

postgresqltimestamp

提问by Expert wanna be

I try to insert a single line to log table, but it throws an error message . ><

我尝试在日志表中插入一行,但它抛出了一条错误消息。><

The logtable structure is like this:

log表的结构是这样的:

no         integer  NOT NULL nextval('log_no_seq'::regclass)    
ip         character varying(50)    
country    character varying(10)    
region     character varying(10)    
city       character varying(50)    
postalCode character varying(10)    
taken      numeric  
date       date

and my query:

和我的查询:

INSERT INTO log (ip,country,region,city,postalCode,taken,date) VALUES 
("24.24.24.24","US","NY","Binghamton","11111",1,"2011-11-09")

=> ERROR: column "postalcode" of relation "log" does not exist

=> ERROR: column "postalcode" of relation "log" does not exist

second try query : (without postalcode)

第二次尝试查询:(没有邮政编码)

INSERT INTO log (ip,country,region,city,taken,date) VALUES 
("24.24.24.24","US","NY","11111",1,"2011-11-09")

=> ERROR: column "24.24.24.24" does not exist

=> ERROR: column "24.24.24.24" does not exist

I don't know what I did wrong...

我不知道我做错了什么...

And PostgreSQL does not have datetime type? (2011-11-09 11:00:10)

而 PostgreSQL 没有 datetime 类型?(2011-11-09 11:00:10)

回答by C. Ramseyer

Try single quotes (e.g. '2011-11-09')

尝试单引号(例如'2011-11-09')

回答by Erwin Brandstetter

PostgreSQL has a "datetime" type: timestamp. Read the manual here.

PostgreSQL 有一个“日期时间”类型:timestamp. 在这里阅读手册

The double-qutes ""are used for identifiers if you want them as is. It's best you never have to use them as @wildplasser advised.

""如果您希望它们原样使用,则双引号用于标识符。最好不要像@wildplasser 建议的那样使用它们。

String literals are enclosed in single quotes ''.

字符串文字用单引号括起来''

Start by reading the chapter Lexical Structure. It is very informative. :)

首先阅读词法结构一章。这是非常有用的信息。:)

回答by Jan Marek

Try it rewrite in this way:

尝试以这种方式重写:

INSERT INTO log (ip,country,region,city,"postalCode",taken,date) VALUES ('24.24.24.24','US','NY','Binghamton','11111',1,'2011-11-09');

INSERT INTO log (ip,country,region,city,"postalCode",taken,date) VALUES ('24.24.24.24','US','NY','Binghamton','11111',1,'2011-11 -09');

When you are using mixed case in the name of column, or reserved words (such as "column", "row" etc.), you have to use double quotes, instead of values, where you have to use a single ones, as you can see in the example.

当您在列名或保留字(如“列”、“行”等)中使用大小写混合时,您必须使用双引号,而不是值,您必须使用单引号,如你可以在例子中看到。