[英]Insertion in database of type timestamp and data containing apostrophe using ORACLE-11g
I have creater the table successfully as follows: 我已经成功地创建了该表,如下所示:
CREATE TABLE TOY_STORE
(
TOY_STORE_ID NUMBER(3) PRIMARY KEY,
TOY_STORE_NAME VARCHAR2(30) NOT NULL,
CITY VARCHAR2(30) DEFAULT 'Delhi',
PHONENUMBER NUMBER(10) NOT NULL UNIQUE,
STORE_OPENING_TIME TIMESTAMP,
STORE_CLOSING_TIME TIMESTAMP
);
ALTER TABLE TOY_STORE ADD CHECK (EXTRACT(HOUR FROM CAST (TO_CHAR (STORE_OPENING_TIME, 'YYYY-MON-DD HH24:MI:SS') AS TIMESTAMP)) > 8 || NULL);
ALTER TABLE TOY_STORE ADD CHECK (EXTRACT(HOUR FROM CAST(TO_CHAR(STORE_CLOSING_TIME, 'YYYY-MON-DD HH24:MI:SS') AS TIMESTAMP)) < 22 || NULL);
Now I want to enter data in the table. 现在我要在表中输入数据。 I executed the following command (here the second data is "Kid's Cave"), 我执行了以下命令(这里的第二个数据是“孩子的洞穴”),
INSERT INTO TOY_STORE VALUES(1, 'Kid''s Cave', 'Delhi', 9912312312, 2014-04-01 09:10:12, 2014-04-01 21:42:05);
But it showed the following error.. 但是它显示了以下错误。
ORA-00917: missing comma
Please explain 请解释
You need to put the dates inside ''
. 您需要将日期放入''
。 Try this: 尝试这个:
INSERT INTO TOY_STORE
VALUES(1, 'Kid''s Cave', 'Delhi', 9912312312, '2014-04-01 09:10:12', '2014-04-01 21:42:05');
On a side note: 附带说明:
I will suggest you to use varchar()
to store PhoneNumbers instead of Number
datatype 我建议您使用varchar()
存储PhoneNumbers而不是Number
数据类型
日期在查询中没有单引号-像这样使用:
'2014-04-01 09:10:12', '2014-04-01 21:42:05'
Try using to_date
to convert string into date
尝试使用to_date
将字符串转换为date
INSERT INTO TOY_STORE
VALUES (1, 'Kid''s Cave', 'Delhi', 9912312312,
to_date('2014-04-01 09:10:12', 'yyyy-mm-dd hh24:mi:ss'),
to_date('2014-04-01 21:42:05', 'yyyy-mm-dd hh24:mi:ss'));
you can also use TIMESTAMP literal
您还可以使用TIMESTAMP literal
INSERT INTO TOY_STORE
VALUES (1, 'Kid''s Cave', 'Delhi', 9912312312,
TIMESTAMP '2014-04-01 09:10:12',
TIMESTAMP '2014-04-01 21:42:05');
The problem isn't the '
, it's the fact that you are not quoting the date literals. 问题不是'
,这是因为您没有引用日期文字。 Oracle interprets it as several integer literals with operators between them, and fails because there's no comma separating them. Oracle将其解释为几个整数,并且它们之间有运算符,但由于没有逗号分隔它们而失败。
Surrounding them by quotes ( '
) should work: 用引号( '
)括起来应该可以:
INSERT INTO TOY_STORE VALUES
(1,
'Kid''s Cave',
'Delhi',
9912312312,
'2014-04-01 09:10:12',
'2014-04-01 21:42:05');
But it's a bad practice, as it assumes the format matches the database's default date format, which makes your code error prone and unprortable. 但这是一个不好的做法,因为它假定格式与数据库的默认日期格式匹配,这会使您的代码易于出错且无法正常运行。 A better approach would be to explicitly convert these values to timestamp
s with an explicitly stated format: 更好的方法是使用明确声明的格式将这些值明确转换为timestamp
:
INSERT INTO TOY_STORE VALUES
(1,
'Kid''s Cave',
'Delhi',
9912312312,
TO_TIMESTAMP('2014-04-01 09:10:12', 'YYYY-MM-DD HH24:MI:SS'),
TO_TIMESTAMP('2014-04-01 21:42:05', 'YYYY-MM-DD HH24:MI:SS'));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.