将 DATE 值插入 Derby 数据库时出现问题
Issues inserting DATE value into Derby database
我正在尝试设置 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 文档的格式相匹配(至少在我看来是这样,因为它不起作用我确定我'哪里弄错了)。
使用 DATE
或 TIMESTAMP
函数,或 JDBC 转义语法,以您喜欢的方式将文字值转换为日期或时间戳数据类型。
或者使用 PreparedStatement
在 Java 中编写插入逻辑,并使用 setDate()
和 setTimestamp()
函数。
上述的各种文档:
DATE
函数:https://db.apache.org/derby/docs/10.15/ref/rrefdatefunc.html
TIMESTAMP
函数:https://db.apache.org/derby/docs/10.15/ref/rreftimestampfunc.html
- JDBC 日期和时间的转义语法:https://db.apache.org/derby/docs/10.15/ref/rrefjdbc1020262.html
PreparedStatement
:
我正在尝试设置 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 文档的格式相匹配(至少在我看来是这样,因为它不起作用我确定我'哪里弄错了)。
使用 DATE
或 TIMESTAMP
函数,或 JDBC 转义语法,以您喜欢的方式将文字值转换为日期或时间戳数据类型。
或者使用 PreparedStatement
在 Java 中编写插入逻辑,并使用 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
: