This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to INSERT with an autoincrement column

 
Carey Brown
Ranch Hand
Pie
Posts: 1324
15
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An autoIncrement column is supposed to be read-only. If that's the case, how does one INSERT a new row? (I'm using MS Access with a JDBC/ODBC bridge)
 
David Peterson
author
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To add a record insert values into all the fields apart from the auto-incrementing one.
 
Carey Brown
Ranch Hand
Pie
Posts: 1324
15
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Peterson:
To add a record insert values into all the fields apart from the auto-incrementing one.

Sorry, doesn't work. If you leave out any of the fields JDBC complains at run-time.
 
Dorothy Finkel-Laverty
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to first figure out what the next "auto increment" number would be - you can use a "get max" first on the column, deal with the result set & getInt(1), the increment it by one (assuming it's an int), then when you insert the new record, you have the correct next value to insert.

[This message has been edited by Dorothy Finkel-Laverty (edited November 24, 2001).]
 
Daniel Dunleavy
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey,
You list columns you are inserting and values.
ex. insert into testtab (lastname, firstname) values('ann', 'jones')
Dan
[This message has been edited by Daniel Dunleavy (edited November 24, 2001).]
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, doesn't work. If you leave out any of the fields JDBC complains at run-time.[/B]

This does work. There may be something else wrong with your insert. What is the error message?
This works:
Table emp, columns id (autonumber), name (string)
sql: insert into emp (name) values ("John Fingers")
this will insert the next autonumber field and the name into the emp table.
Jamie
 
sandeep balyan
Ranch Hand
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
my friend make one oracle sequence it will work i Solved it before
sandeep
 
Jamie Robertson
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by sandeep balyan:
my friend make one oracle sequence it will work i Solved it before
sandeep

tough to make an Oracle sequence when you are using MSAccess Autonumber!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic