| От | Nikola Milutinovic |
|---|---|
| Тема | How to get automatically generated "id" field |
| Дата | |
| Msg-id | 42336C35.7080003@ev.co.yu обсуждение исходный текст |
| Ответы |
Re: How to get automatically generated "id" field
|
| Список | pgsql-jdbc |
Hi all.
This may be a bit more for general ML, but I'm in a fix and need an
advice. I have several tables with auto-generated "id" field, like this.
CREATE TABLE photo (
id SERIAL PRIMARY KEY,
size INT8,
file VARCHAR(256)
)
I would like to be able to insert values and get back the "id" of a
newly inserted record. What is a good way to do it?
I imagine I could open a transaction and read the value of the sequence,
but that is ugly, since the sequence name is autogenerated and PG specific.
Would it be better to change "SERIAL" to just INT8 and do in a transaction:
...start transaction
executeQuery( "SELECT max( id )+1 AS new_id FROM photo" );
..read "new_id"
executeUpdate( "INSERT INTO photo (id, size, file) VALUES (...)" );
..commit
Nix.
В списке pgsql-jdbc по дате отправления:
Сайт использует файлы cookie для корректной работы и повышения удобства. Нажимая кнопку «Принять» или продолжая пользоваться сайтом, вы соглашаетесь на их использование в соответствии с Политикой в отношении обработки cookie ООО «ППГ», в том числе на передачу данных из файлов cookie сторонним статистическим и рекламным службам. Вы можете управлять настройками cookie через параметры вашего браузера