java.sql.SQLException: 字段 'id' 没有默认值

java.sql.SQLException: Field 'id' doesn't have a default value

我正在尝试将数据插入 arrivaltimes 表,但出现以下错误:

java.sql.SQLException: Field 'id' doesn't have a default value

stt.execute("CREATE TABLE IF NOT EXISTS stops"
            + "(stop_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, "
            + " name varchar(30) NOT NULL, "
            + " route INT(11) NOT NULL, "
            + " lat double(10,6) NOT NULL, "
            + " longi double(10,6)NOT NULL) " );

    stt.execute("INSERT INTO stops(name, route, lat, longi) values"
            + "('blabla', '1', '93.838039', '15.700440' ),"
            + "('backery', '9', '98.868863', '19.665438' )" );

    stt.execute("CREATE TABLE IF NOT EXISTS arrivaltimes(id INT(11) NOT NULL PRIMARY KEY,"
            +  " weekday VARCHAR(20) NOT NULL,"
            + "arrivaltime time NOT NULL,"
            + " stop_id INT, FOREIGN KEY fk_stop_id(stop_id) REFERENCES stops(stop_id) )" );
    //The error appears in this execution statement.
    stt.execute("INSERT INTO arrivaltimes(weekday, arrivaltime) values"
            + "('mon-fri', '05:30' ),"
            + "('mon-fri', '06:07' )" );

因为列 id 被定义为 NOT NULL,这意味着它必须有一个值,而您没有提供一个值。

您缺少 AUTO INCREMENT 到达时间 table 的主键。只需在创建 table

时添加 AUTO_INCREMENT
stt.execute("CREATE TABLE IF NOT EXISTS arrivaltimes(id INT(11) NOT NULL  AUTO_INCREMENT PRIMARY KEY,"
            +  " weekday VARCHAR(20) NOT NULL,"
            + "arrivaltime time NOT NULL,"
            + " stop_id INT, FOREIGN KEY fk_stop_id(stop_id) REFERENCES stops(stop_id) )" );

我在 MySQL WorkBench 中的 table 遇到了同样的问题。显然,是自动递增命令为字段 'id' 赋予了默认值。这是适用于我的 table:

的代码
create table product( <br>
id int auto_increment PRIMARY KEY, <br>
name varchar(20), <br>
description varchar(100), <br>
price decimal(8,3) <br>
);