我正在尝试设置一个derby数据库,并将一些示例信息插入其中进行测试。
获取我想要的表的代码是。。。
CREATE TABLE users(
userid varchar(128) primary key,
passwd_digest varchar(128)
);
CREATE TABLE customers(
cust_id int not null primary key(START WITH 1, INCREMENT BY 1),
cust_name varchar(128)
);
CREATE TABLE orders(
order_id int not null primary key,
cust_id int not null(START WITH 1, INCREMENT BY 1),
foreign key(cust_id) references customers(cust_id),
order_date date,
order_desc varchar(128)
);
现在,当我尝试使用以下代码将示例数据插入其中时:
insert into customers(cust_id, cust_name) values (3, 'Ringo');
insert into orders(order_id, cust_id, order_date, order_desc) values (4, 3, 12.12.1957, 'A drumset');
我从ij得到以下错误:
ERROR 42X01: Syntax error: Encountered ".1957" at line 1, column 82.
Issue the 'help' command for general information on IJ command syntax.
Any unrecognized commands are treated as potential SQL commands and executed directly.
Consult your DBMS server reference documentation for details of the SQL syntax supported by your server.
我试着用以下方式放置日期值(忽略其他数据,只有日期看起来不起作用):
insert into orders(order_id, cust_id, order_date, order_desc) values (4, 3, 1957-12-12 00:00:00:000, 'A drumset');
insert into orders(order_id, cust_id, order_date, order_desc) values (4, 3, 1957-12-12, 'A drumset');
insert into orders(order_id, cust_id, order_date, order_desc) values (4, 3, "12-12-1957", 'A drumset');
insert into orders(order_id, cust_id, order_date, order_desc) values (4, 3, DATE("12-12-1957"), 'A drumset');
insert into orders(order_id, cust_id, order_date, order_desc) values (4, 3, 12-12-1957, 'A drumset');
但它们都会产生类似的错误,包括:
ERROR 42821: Columns of type 'DATE' cannot hold values of type 'INTEGER'.
我不知道为什么会出现这些错误,因为我输入了与derby文档格式匹配的值(至少在我看来是这样的,因为它不起作用,所以我肯定我在某个地方弄错了)。
1条答案
按热度按时间cnjp1d6j1#
使用
DATE
或者TIMESTAMP
函数或jdbc转义语法,以将文本值转换为日期或时间戳数据类型。或者使用
PreparedStatement
,并使用setDate()
以及setTimestamp()
功能。以上各项文件:
DATE
功能:https://db.apache.org/derby/docs/10.15/ref/rrefdatefunc.htmlTIMESTAMP
功能:https://db.apache.org/derby/docs/10.15/ref/rreftimestampfunc.html日期和时间的jdbc转义语法:https://db.apache.org/derby/docs/10.15/ref/rrefjdbc1020262.html
PreparedStatement
: https://stackoverflow.com/a/18615191/193453