Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

expected 0 arguments, got 1

Tags:

mysql

go

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.

like image 981
Ricky Manalo Avatar asked Mar 08 '23 16:03

Ricky Manalo


1 Answers

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)
like image 130
mkopriva Avatar answered Mar 15 '23 19:03

mkopriva