我试图让表中的第一列只是在添加新行时自动递增,而是在那里插入“ NULL ”。
以下是我插入新行的代码:
String update = "INSERT INTO help(name, area, date, message) VALUES(?, ?, ?, ?)";
try {
connection = plugin.getHikari().getConnection();
// Inserting into the table
statement = connection.prepareStatement(update);
// Replace the '?' with the actual information
statement.setString(1, name);
statement.setString(2, area);
statement.setString(3, date);
statement.setString(4, message);
statement.execute();
} catch (SQLException e) {
e.printStackTrace();
}
以下是我创建表格的方法:
statement = connection.prepareStatement(
"CREATE TABLE IF NOT EXISTS help" +
"(" +
"id int," +
"name varchar(20)," +
"area varchar(25)," +
"date varchar(15)," +
"message varchar(255)" +
")");
statement.execute();
以下是每次加载/启动应用程序时运行的声明:
statement = connection.prepareStatement("ALTER TABLE help MODIFY COLUMN id INT auto_increment");
谢谢, - Nicster
答案 0 :(得分:2)
您可以在create语句中执行所需的所有操作。问题是你的id列可以为空并且不是主键。
只需将您的创建语句更改为
即可statement = connection.prepareStatement(
"CREATE TABLE IF NOT EXISTS help" +
"(" +
"id int not null auto_increment," +
"name varchar(20)," +
"area varchar(25)," +
"date varchar(15)," +
"message varchar(255)" +
"PRIMARY KEY (id)" +
")");
statement.execute();
您不应该需要alter table语句。希望有所帮助
答案 1 :(得分:0)
要么将主键添加到表“PRIMARY KEY(id))”这将自动增加,或者每次插入行时都必须使用java逻辑来增加值