如何正确使用自增

How to properly use autoincrement

我正在尝试创建一个帐户数据库,该数据库在每次新插入后自动增加 1,但是当我尝试向 table 中插入内容时,它会抛出此错误。

org.h2.jdbc.JdbcSQLException: Syntax error in SQL statement "INSERT INTO ACCOUNT (USERACCNAME=[*]'admin',USERIP='lol', USEREMAIL='somehing', USERPASSWD='74123', USERPREMIUM='-1', CHAR1='1', CHAR2='-1', CHAR3='-1', CHAR4='-1') "; expected ",, )"; SQL statement:
INSERT INTO account (userAccName='admin',userIP='lol', userEmail='somehing', userPasswd='74123', userPremium='-1', char1='1', char2='-1', char3='-1', char4='-1') [42001-171]

这是导致错误的插入

updateDatabase("INSERT INTO account VALUES(" +
                "userAccName='admin'," +
                "userIP='lol', " +
                "userEmail='somehing', " +
                "userPasswd='74123', " +
                "userPremium='-1', " +
                "char1='1', " +
                "char2='-1', " +
                "char3='-1', " +
                "char4='-1')");

我试过插入 table 包括 userAccID

updateDatabase("INSERT INTO account VALUES(" +
                "userAccID='1'," +
                "userAccName='admin'," +
                "userIP='lol', " +
                "userEmail='somehing', " +
                "userPasswd='74123', " +
                "userPremium='-1', " +
                "char1='1', " +
                "char2='-1', " +
                "char3='-1', " +
                "char4='-1')");

但是当我 运行 它

它抛出这个
org.h2.jdbc.JdbcSQLException: Column "USERACCID" not found; SQL statement:
INSERT INTO account VALUES(userAccID='1',userAccName='admin',userIP='lol',userEmail='somehing', userPasswd='74123', userPremium='-1', char1='1', char2='-1', char3='-1', char4='-1') [42122-171]

SQL 结构如下所示

create table if not exists account(
    userAccID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    userAccName VARCHAR(20) NOT NULL,
    userIP VARCHAR(16) NOT NULL DEFAULT '-1',
    userEmail VARCHAR(64) NOT NULL DEFAULT '-1',
    userPasswd VARCHAR(64) NOT NULL DEFAULT '-1',
    userPremium INT NOT NULL DEFAULT '-1',
    char1 INT NOT NULL DEFAULT '-1',
    char2 INT NOT NULL DEFAULT '-1',
    char3 INT NOT NULL DEFAULT '-1',
    char4 INT NOT NULL DEFAULT '-1'
);

这就是 UpdateDatabase 方法的样子(如果它是问题的根源)

public static int updateDatabase(String statement){
    int SQL_Status = -1;
    Statement SQL_Statement = null;
    try {
        SQL_Statement = connection.createStatement();
        SQL_Statement.setQueryTimeout(20);  // set timeout to 20 sec.
        SQL_Status = SQL_Statement.executeUpdate(statement);
        connection.commit();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return SQL_Status;
}

基本上我正在尝试将其结构化为这样

id=1, accName=admin, userPasswd=x
id=2, accName=user1, userPasswd=y
id=3, accName=user2, userPasswd=z

我认为问题是您使用引号将整数输入到 INT 类型的列中?这意味着您要将字符串输入到整数类型的列中?

试试这个..也许有用

"INSERT INTO account VALUES(" +
            "userAccName='admin'," +
            "userIP='lol', " +
            "userEmail='somehing', " +
            "userPasswd='74123', " +
            "userPremium= -1, " +
            "char1= 1, " +
            "char2= -1, " +
            "char3= -1, " +
            "char4= -1 )"

原回答:

您可以像这样创建 table 吗?注意IDENTITY的使用...

create table if not exists account( userAccID IDENTITY, userAccName VARCHAR(20) NOT NULL, userIP VARCHAR(16) NOT NULL DEFAULT '-1', userEmail VARCHAR(64) NOT NULL DEFAULT '-1', userPasswd VARCHAR(64) NOT NULL DEFAULT '-1', userPremium INT NOT NULL DEFAULT '-1', char1 INT NOT NULL DEFAULT '-1', char2 INT NOT NULL DEFAULT '-1', char3 INT NOT NULL DEFAULT '-1', char4 INT NOT NULL DEFAULT '-1' );

编辑后的答案:

我明白了:您试图插入所有属性,甚至是主键 useraccid。通常是个坏主意。让我解释一下为什么...

当创建新的SQL记录时,如果这样的主键是使用自增策略创建的,你只需要插入任何其他属性(前提是你不违反SQL约束,如NOT NULL) 并且将通过内部计数器创建一个新的 useraccid。

如果您需要 modify/update 现有记录,则需要执行过滤器 WHERE 但您将仅更新修改后的属性,而 useraccid 将保持不变。如有疑问请参考H2手册(http://h2database.com/html/grammar.html#update)

因此,如果您需要在请求中插入一条没有主键 useraccid 的新记录,那么 h2 的正确 SQL 命令是:

INSERT INTO account (userAccName,userIP,userEmail,userPasswd,userPremium,char1,char2,char3,char4) VALUES ('admin','lol','somehing','74123',-1,1,-1,-1,-1)

请注意,在 H2 web 控制台中,您可以尝试以下循环命令,插入 100 次相同的值并创建 100 条记录:

@loop 100 INSERT INTO account (userAccName,userIP,userEmail,userPasswd,userPremium,char1,char2,char3,char4) VALUES ('admin','lol','somehing','74123',-1,1,-1,-1,-1)