mysql - How to insert a tuple only if it is not available in the table in Golang? -
in use case, want write function insert row in table in golang if row not available already. using mysql , github.com/go-sql-driver/mysql. getting error while try insert through function. able insert running through mysql console. here function definition,
func addtag(t *objects.tag) { //get connection object db, err := getdbconnection() defer db.close() // prepare statement inserting data stmtins, err := db.prepare("insert tag (tag_id, tag_name, metatag_id) select * (select null, ?, ?) tmp not exists ( select tag_name tag tag_name = ? ) limit 1") if err != nil { fmt.println("fatal ", time.now(), "error in prepare statement tag insert ") panic(err.error()) } defer stmtins.close() // close statement when leave main() / program terminates _, err = stmtins.exec(t.tagname, t.metatagid, t.tagname) // insert tuples if err != nil { fmt.println("fatal ", time.now(), "error in insert tag ") panic(err.error()) } }
edit 1 : getting following error,
fatal 2015-06-14 17:13:15.631487042 -0700 pdt error in insert tag panic: error 1136: column count doesn't match value count @ row 1 goroutine 1 [running]: main.addtag(0x1112e1a0, 0x0, 0x0)
could me on this? thank you
"column count doesn't match value count" means didn't provide value column in table doesn't have default value.
check table schema (describe tag;
) , ensure providing values other columns, or have default value.
Comments
Post a Comment