本文介绍了java.sql.SQLException:字段“id"没有默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to insert data into arrivaltimes tables but I am getting the following error:

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' )" );

推荐答案

您在到达时间表中缺少主键的 AUTO INCREMENT.只需要在创建表时添加AUTO_INCREMENT

You are missing AUTO INCREMENT for Primary Key in arrivaltimes table. Just need to add AUTO_INCREMENT while creating table

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) )" );

这篇关于java.sql.SQLException:字段“id"没有默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-12 09:20