Error: multiple-value db.Exec() in single-value context

1,218 views
Skip to first unread message

Klim

unread,
May 31, 2014, 11:14:13 AM5/31/14
to golan...@googlegroups.com

I am new to golang. Can some point me to the right direction with this error.
When attempting to insert json data into a database table, I received error “multiple-value db.Exec() in single-value context.

Here is a simple example to reproduce the problem.

//postgres ddl:
create table signup (id big serial primary key, json jsonb);

//go code:
package main

import (
    "database/sql"
    _ "github.com/lib/pq"
    "log"
)

func main() {
    db, err := sql.Open("postgres", "user=johndoe password='password' dbname=test sslmode=disable")
    if err != nil {
        log.Fatal(err)
    }

    //mockup data
    data := `{"Email": “joh...@gmail.com"}`

    //insert
    err = db.Exec("insert into signup (data) values ($1)", data)
    if err != nil {
        log.Fatal(err)
    }
}

Gyepi SAM

unread,
May 31, 2014, 2:59:00 PM5/31/14
to Klim, golan...@googlegroups.com
On Sat, May 31, 2014 at 08:14:13AM -0700, Klim wrote:
> I am new to golang. Can some point me to the right direction with this
> error.
> When attempting to insert json data into a database table, I received error
> “multiple-value db.Exec() in single-value context.

db.Exec() returns multiple values but you're assigning the results
to a single variable.

-Gyepi

Klim

unread,
May 31, 2014, 4:12:02 PM5/31/14
to golan...@googlegroups.com, m...@kennylim.com, self-...@gyepi.com

Hi Gyepi, can you show me a brief code example on how to handle that? Thanks.

Matt Silverlock

unread,
May 31, 2014, 9:42:37 PM5/31/14
to golan...@googlegroups.com
http://golang.org/pkg/database/sql/#DB.Exec

"Multiple value X in single value context" always means you have more than one return value. You have to capture the return value or at least explicitly ignore it.

I recommend using sqlx to make dealing with Go's database/sql interface a little easier https://github.com/jmoiron/sqlx + first reading http://go-database-sql.org/

Klim

unread,
May 31, 2014, 11:17:36 PM5/31/14
to golan...@googlegroups.com

Thanks Matt ! Appreciate it.
Reply all
Reply to author
Forward
0 new messages