1

I am trying to insert incoming form values into my PostgreSQL database table on Heroku. This is the Go function being used:

func Insert(w http.ResponseWriter, r *http.Request) {
    db := dbConn()
    if r.Method == "POST" {
        nameo := r.FormValue("name")
        typeo := r.FormValue("asslia")
        balanceo := r.FormValue("balance")
        insForm, err := db.Prepare("INSERT INTO al(name, asslia, balance) VALUES( ? , ? , ? )")
        if err != nil {
            panic(err.Error())
        }
        insForm.Exec(nameo, typeo, balanceo)
        log.Println("INSERT: Name: " + nameo + " | Type: " + typeo)
    }
    defer db.Close()
    http.Redirect(w, r, "/", 301)
}

I keep getting the error pq: syntax error at or near "," at the line insForm, err := db.Prepare("INSERT INTO al(name, asslia, balance) VALUES( ? , ? , ? )")

The table was previously created as such:

CREATE TYPE types AS ENUM ('asset', 'liability');

CREATE TABLE IF NOT EXISTS al 
(id SERIAL, 
asslia TYPES, 
balance MONEY, 
name VARCHAR(64) NOT NULL UNIQUE, 
CHECK (CHAR_LENGTH(TRIM(name)) > 0));

What am I doing wrong?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
RockAndaHardPlace
  • 417
  • 1
  • 7
  • 18

1 Answers1

0

The right way of doing this is to replace the ? with $N where N is the index of the argument that is passed in as extra arguments to db.Exec()

This new statement does not cause an error:

insForm, err := db.Prepare("INSERT INTO al(name, asslia, balance) VALUES( $1 , $2 , $3 )")
RockAndaHardPlace
  • 417
  • 1
  • 7
  • 18