2

I am trying to insert a string to a Postgres database. And I couldn't find the correct syntax. Here is the code:

func insertdb() {
    fmt.Println("Write your text")
    var input string
    fmt.Scanln(&input)
    insertstmt := `insert into "todos"("do_info") values(**I need this**)`
    _, e := DB.Exec(insertstmt)
    checkError(e)
}

I want to insert the input variable to my Postgresql database. How should I write it after values in sql query?

values($1)

Error says need a parameter.

1 Answer 1

5

Your code complains beacause the query has a placeholder $1 and it does not have a matching argument passed to the Exec function.

You have to pass the input to the Exec function so that it can replace the placeholder. i.e:

    fmt.Println("Write your text")
    var input string
    fmt.Scanln(&input)
    insertstmt := `insert into todos (do_info) values($1)`
    _, err = DB.Exec(insertstmt, input)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.