1
praynise 2019-05-06 17:04:47 +08:00
好使啊,以下是我测试的代码,可以正常执行
package main import ( "database/sql" "fmt" _ "github.com/lib/pq" ) func main() { psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+ "password=%s dbname=%s sslmode=disable", "localhost", 5432, "postgres", "postgres", "postgres") db, err := sql.Open("postgres", psqlInfo) if err != nil { panic(err) } defer db.Close() err = db.Ping() if err != nil { panic(err) } sql := "insert into testupsert values ($1,$2,$3) on conflict (uid) do update set total=testupsert.total+1,what = EXCLUDED.what" _,err = db.Exec(sql,1,3,3) if err != nil{ panic(err) } } |
2
imherer OP |
4
praynise 2019-05-07 10:58:33 +08:00
EXCLUDED 代表从外部来的,就是你 value 中的值。在我的例子中,EXCLUDED.what 代表就是$3。我忘了付上我得表结构了
|