将日期插入SQLite表

时间:2018-09-30 07:09:05

标签: sql macos sqlite visual-studio-code

我是SQL的新手,我想创建一个学生表,其中包含学生的出生日期。这是我的代码。我在Visual Studio Code上运行SQLite

create table student
(std_code             varchar(8),
std_fname            varchar(15)    constraint student_std_lname_nn not null,
std_lname            varchar(15)    constraint student_std_fname_nn not null,
std_gend              varchar(8),   
maj_code              varchar(10)   constraint student_maj_code_fk references                 
major (maj_code),
std_dob                date, 
constraint student_std_code_pk primary key (std_code));

insert into student values ('S01', 'Michael', 'Jordan', 'M', 'FINC', date(1962- 
03-10));
insert into student values ('S02', 'Charles', 'Barkley', 'M', null, date(1964- 
09-12));

代码运行没有错误,但是当我运行SELECT * FROM student时,日期看起来都是错误的。 enter image description here

任何帮助解决该问题的方法将不胜感激。谢谢

1 个答案:

答案 0 :(得分:1)

您需要使用单引号'包含日期字符串。

  

date(时间字符串,修饰符,修饰符...)

因此,您需要传递DateTime字符串作为参数。

insert into student values ('S01', 'Michael', 'Jordan', 'M', 'FINC',
                            '1962-03-10');
insert into student values ('S02', 'Charles', 'Barkley', 'M', null, 
                            '1964-09-12');

或仅使用Date字符串

insert into student values ('S01', 'Michael', 'Jordan', 'M', 'FINC',
                            '1962-03-10');
insert into student values ('S02', 'Charles', 'Barkley', 'M', null, 
                            '1964-09-12');

查询#1

select * from student;

| std_code | std_fname | std_lname | std_gend | maj_code | std_dob    |
| -------- | --------- | --------- | -------- | -------- | ---------- |
| S01      | Michael   | Jordan    | M        | FINC     | 1962-03-10 |
| S02      | Charles   | Barkley   | M        |          | 1964-09-12 |

View on DB Fiddle

相关问题