1

I am getting an error when I try to use ? arguments when using the go sql library.

Whe I do something like:

_, err = db.Query(
    `INSERT INTO user_account (username, password_hash) VALUES ("username", 'passwordhash');`,

everything works fine.

But when I do the following:

_, err = db.Query(
    `INSERT INTO user_account (username, password_hash) VALUES (?, 'passwordhash');`,
    `'username'`)

I get the error:

pq: syntax error at or near ","

How can I fix this error? I suspect it has something to do with the single quotes around the variables.

If it matters, I'm using postgreq with the pq driver.

6
  • have you tried username instead of 'username'? The qp package will escape the value by itself. Commented Mar 28, 2018 at 20:02
  • What is happening if you use double quotes for username like: , err = db.Query( INSERT INTO user_account (username, password_hash) VALUES (?, 'passwordhash');, "username") Commented Mar 28, 2018 at 20:03
  • On the website I see in the examples no ? but $1, $2, etc. Maybe try that. Commented Mar 28, 2018 at 20:05
  • Thanks for the help. I fixed this by using the $1 syntax (thanks TehSphinx). Commented Mar 28, 2018 at 20:09
  • Once this was fixed, I had a double quote issue as well. The string "'username'" was being added isntead of the string "username". Commented Mar 28, 2018 at 20:10

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.