sql - How to properly use autoincrement -


i trying create database of accounts automatically increase 1 after every new insertion, when try insert table throws error.

org.h2.jdbc.jdbcsqlexception: syntax error in sql statement "insert account (useraccname=[*]'admin',userip='lol', useremail='somehing', userpasswd='74123', userpremium='-1', char1='1', char2='-1', char3='-1', char4='-1') "; expected ",, )"; sql statement: insert account (useraccname='admin',userip='lol', useremail='somehing', userpasswd='74123', userpremium='-1', char1='1', char2='-1', char3='-1', char4='-1') [42001-171] 

this insert causes error

updatedatabase("insert account values(" +                 "useraccname='admin'," +                 "userip='lol', " +                 "useremail='somehing', " +                 "userpasswd='74123', " +                 "userpremium='-1', " +                 "char1='1', " +                 "char2='-1', " +                 "char3='-1', " +                 "char4='-1')"); 

i have tried entering inserting table including useraccid

updatedatabase("insert account values(" +                 "useraccid='1'," +                 "useraccname='admin'," +                 "userip='lol', " +                 "useremail='somehing', " +                 "userpasswd='74123', " +                 "userpremium='-1', " +                 "char1='1', " +                 "char2='-1', " +                 "char3='-1', " +                 "char4='-1')"); 

however throws when run it

org.h2.jdbc.jdbcsqlexception: column "useraccid" not found; sql statement: insert account values(useraccid='1',useraccname='admin',userip='lol',useremail='somehing', userpasswd='74123', userpremium='-1', char1='1', char2='-1', char3='-1', char4='-1') [42122-171] 

the sql structure looks this

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

this updatedatabase method looks (in case source of problem)

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 20 sec.         sql_status = sql_statement.executeupdate(statement);         connection.commit();     } catch (sqlexception e) {         e.printstacktrace();     }     return sql_status; } 

basically trying have structured this

id=1, accname=admin, userpasswd=x id=2, accname=user1, userpasswd=y id=3, accname=user2, userpasswd=z 

original answer:

could create table instead? note use of 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' );

edited answer:

i see: trying insert attributes, primary key useraccid. bad idea. let me explain why...

when creating new sql records, if such primary key created autoincrement policy, need insert other attributes (provided not violate sql constraint not null) , new useraccid created through internal counter.

if need modify/update existing record, you'll need perform filter where you'll update modified attributes , useraccid remain same. please refer h2 manual (http://h2database.com/html/grammar.html#update) if in doubts.

so if need insert new record without primary key useraccid in request, correct sql command h2 is:

insert account (useraccname,userip,useremail,userpasswd,userpremium,char1,char2,char3,char4) values ('admin','lol','somehing','74123',-1,1,-1,-1,-1)

please note in h2 web console, can try following loop command, inserting 100 times same values , creating 100 records:

@loop 100 insert account (useraccname,userip,useremail,userpasswd,userpremium,char1,char2,char3,char4) values ('admin','lol','somehing','74123',-1,1,-1,-1,-1)


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -