ck_test.go 918 B

12345678910111213141516171819202122232425262728293031323334353637
  1. package main
  2. import (
  3. "fmt"
  4. "gorm.io/driver/clickhouse"
  5. "gorm.io/gorm"
  6. "log"
  7. "net/url"
  8. "testing"
  9. )
  10. func TestUpdateCk(T *testing.T) {
  11. username := "jytop"
  12. password := "pwdTopJy123"
  13. host := "192.168.3.207:19000"
  14. encodedPassword := url.QueryEscape(password)
  15. dn := fmt.Sprintf("clickhouse://%s:%s@%s/information?dial_timeout=10s&read_timeout=20s", username, encodedPassword, host)
  16. db, err := gorm.Open(clickhouse.Open(dn), &gorm.Config{})
  17. if err != nil {
  18. log.Fatal("链接数据库失败")
  19. } else {
  20. log.Println("数据库连接成功", db.Name())
  21. }
  22. exist := EntMapCode{}
  23. where := EntMapCode{AName: "北京森嘉家具发展公司", BName: "北京森嘉义东家具有限公司"}
  24. db.Where(&where).First(&exist)
  25. if exist.CreateTime > 0 {
  26. //存在数据,需要更新
  27. update := EntMapCode{
  28. InvestRatio: "0.3",
  29. InvestPrice: "30",
  30. }
  31. db.Model(&exist).Where(&where).Updates(&update)
  32. }
  33. }