预期0个参数,得到1个

I'm getting an error which I can't see any fix on the internet. I am new to go-lang and I can't see any error in my SQL statement. I am executing an update query in go-lang. the code is:

sql = "UPDATE tablename SET id1='' WHERE id2=" +fmt.Sprint(v.acctId)
_, err = db.Exec(sql, nil)

id1 and id2 are both varchar. I put the SQL statement in a string then execute it. I tried removing one of the ids but it still keeps showing the error. Also what I noticed is that the value of v.acctId isn't always the same for some reason. I don't know why this happens because when I tried the SQL statement on MySQL workspace, it works fine.

db.Exec has the following signature

func (db *DB) Exec(query string, args ...interface{}) (Result, error)

Args is a varargs parameter, meaning that you can put as many data as you would like. However, for each variable you pass to Exec, there should be a "?" in your sql query. The reason you're getting the error is that you're passing it nil, where you should pass it nothing.

Change

err = db.Exec(sql, nil)

To

err = db.Exec(sql)

Alternatively you can refactor your query to the following

sql = "UPDATE tablename SET id1='' WHERE id2=?"

Then run exec like so

err = db.Exec(sql, fmt.Sprint(v.acctId))

You're getting the error because you're passing in nil as the second argument to Exec while your query has no parameter placeholders ($1 in postgres, ? in mysql i believe) specified.

Instead do something like this

db.Exec("UPDATE tablename SET id1='' WHERE id2=?", v.acctId)

Also, you should almost never construct your queries by concatenating strings as that leads to SQL injection. So please use the param placeholders.

In case you want to execute a query that has no parameters then do what you're doing just do not pass nil as the second argument.

db.Exec(sql)