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.
CodePudding user response:
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)