Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang sql named parameter query

Tags:

mysql

go

I am trying to use named parameter query in golang as follows:

stmt, err := db.Prepare("insert into users(name, email) values(@name, @email)")
if err != nil {
    //error handling
}

res, err := stmt.Exec(sql.Named("name", name), sql.Named("email", email))

if err != nil {
        //error: sql: expected 0 arguments, got 2 
}

I get the following error: "sql: expected 0 arguments, got 2".

What is correct way to write this query?

like image 244
kargirwar Avatar asked Oct 08 '17 06:10

kargirwar


1 Answers

Go needs to support every kind of SQL server - and not all SQL servers support named parameters. The servers that do support them do it with a variety of quirks and "gotchas". But they all support positional parameters just fine.

However there is an open issue for it for the mysql-driver: https://github.com/go-sql-driver/mysql/issues/561

like image 79
Adrian Forsius Avatar answered Sep 29 '22 07:09

Adrian Forsius