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.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With