Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generated Keys #1

Closed
GeoffOs opened this issue Oct 26, 2010 · 2 comments
Closed

generated Keys #1

GeoffOs opened this issue Oct 26, 2010 · 2 comments

Comments

@GeoffOs
Copy link

GeoffOs commented Oct 26, 2010

apologies if this is incorrect...

Support on Execute for returning any generated keys as the result of statement. An example in Java would be:

Statement stmt = conn.createStatement();
stmt.execute(sql, Statement.RETURN_GENERATED_KEYS);
ResultSet keyset = stmt.getGeneratedKeys();
@lxn
Copy link
Owner

lxn commented Oct 27, 2010

Hi Geoff,

You could do something like this if your table has a serial pk named id and you insert a single record:
...
var id int
_, err := conn.Scan("INSERT INTO foo (bar) VALUES ('baz') RETURNING id;", &id)
if err != nil {
return err
}
// do something with id
...

The Scan method is available on Conn as well as Statement.

If you insert multiple records at once, you may want to try one of the Query methods instead (didn't test this one though).

I'm closing this issue, but feel free to reopen, if it doesn't work out.

Thanks
Alex

@GeoffOs
Copy link
Author

GeoffOs commented Oct 28, 2010

I did not know about that option in postgres.

Have got it working on your suggestion thanks.

cbbrowne pushed a commit to cbbrowne/go-pgsql that referenced this issue Dec 8, 2011
Added a bit more to the examples
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants