简体   繁体   中英

How to auto increment database column in Java derby?

Here's the SQL I've tried so far to create the table

CREATE TABLE tblPosts (nId INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
strContent VARCHAR(140) NOT NULL,
strLink VARCHAR(200),
strImage VARCHAR(200));

using

String sql = "INSERT INTO tblPosts VALUES ('" + textFieldContent.getText() + "', '" + 

textFieldLink.getText() + "', '" + textFieldImage.getText() + "')";

I get an error telling me I'm not providing the nId column value which I'm not but if the column auto-increments I'm not supposed to right?

I've also tried using the IDE to create the table on the database as described here

Alter a table column with auto increment by 1 in derby

Same response though. Any suggestions?

You need to include Start with 1, Increment by 1 Like this

CREATE TABLE tblPosts (nId INTEGER PRIMARY KEY GENERATED ALWAYS AS IDENTITY(Start with 1, Increment by 1), 
strContent VARCHAR(140) NOT NULL, 
strLink VARCHAR(200)

我猜想,因为您没有在 SELECT 中指定列名,所以对数据应该进入哪些列感到困惑。我会在您的 INSERT 命令中指定列名。

You need to set THAT auto_increment column to DEFAULT like this:

String sql = "INSERT INTO tblPosts VALUES ( DEFAULT, '" + textFieldContent.getText() + "', '" + 

textFieldLink.getText() + "', '" + textFieldImage.getText() + "')";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM