FAQ
Just what I was looking for too, thanks =)
On Tuesday, May 8, 2012 3:19:13 AM UTC-6, Alexander Neumann wrote:

Try something like this (untested):

err := db.QueryRow("INSERT INTO foo (bar) VALUES ($1) RETURNING id;",
bar).Scan(&id)
On 8 Mai, 07:18, zeroc8 wrote:
Has anyone been successful getting the LastInsertId after an insert
statement?

When I try to read LastInsertId() from result, I get
"pq: this is postgres, a real database, this isn't a valid command".

Fair enough, but Postgres has something like this
insert into foo values('bla') return id

Is there any way to retrieve that value?
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Arne Hormann at Jul 10, 2013 at 8:26 am
    I never used the pg driver, but why don't you use Exec instead of Query?
    Exec returns sql.Result <http://golang.org/pkg/database/sql/#Result>, which
    has a method LastInsertId().

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Notedit at Jul 10, 2013 at 8:51 am
    the pg driver's sql.Result does not return lastinsertid.

    err := db.QueryRow("INSERT INTO foo (bar) VALUES ($1) RETURNING id;",
    bar).Scan(&id)

    this will have another question, if the insert has some error, it will
    return some like "no result error".


    2013/7/10 Arne Hormann <arnehormann@gmail.com>
    I never used the pg driver, but why don't you use Exec instead of Query?
    Exec returns sql.Result <http://golang.org/pkg/database/sql/#Result>,
    which has a method LastInsertId().

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
  • Kamil Kisiel at Jul 10, 2013 at 4:12 pm
    Postgres by default does not return the last inserted id unless you
    explicitly ask for it with the RETURNING clause. However the fields
    returned by the clause can be arbitrarily specified, so there's no way to
    identify a single id without knowledge of the schema and query.

    See http://www.postgresql.org/docs/9.2/static/sql-insert.html
    On Wednesday, July 10, 2013 1:26:21 AM UTC-7, Arne Hormann wrote:

    I never used the pg driver, but why don't you use Exec instead of Query?
    Exec returns sql.Result <http://golang.org/pkg/database/sql/#Result>,
    which has a method LastInsertId().
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJul 9, '13 at 11:04p
activeJul 10, '13 at 4:12p
posts4
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase