我正在尝试将数据插入arrivaltimes
表中,但我收到以下错误:
java.sql.SQLException:字段'id'没有默认值
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' )" );
答案 0 :(得分:4)
您到达时间表中的主键缺少AUTO INCREMENT
。只需在创建表格时添加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) )" );
答案 1 :(得分:1)
因为列id
被定义为NOT NULL
,这意味着它必须有一个值,而您却没有提供一个值。
答案 2 :(得分:0)
我在MySQL WorkBench中的表遇到了同样的问题。显然,是自动递增命令为字段“ id”赋予了默认值。这是适用于我的表的代码:
create table product( <br>
id int auto_increment PRIMARY KEY, <br>
name varchar(20), <br>
description varchar(100), <br>
price decimal(8,3) <br>
);