db_mysql.go 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. package main
  2. import (
  3. "database/sql"
  4. "fmt"
  5. _ "github.com/go-sql-driver/mysql"
  6. )
  7. var mysqlDb *sql.DB
  8. func regMysql(brand string) {
  9. //const mySqlUrl = "%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&loc=Local"
  10. url := ""
  11. switch brand {
  12. case "ugg":
  13. url = fmt.Sprintf("root:plmPLM0.@tcp(%s.imix7.com:13306)/uggwb?charset=utf8mb4&loc=Local", brand)
  14. case "hdugg":
  15. url = fmt.Sprintf("root:plmPLM0.@tcp(%s.imix7.com:13306)/hduggwb?charset=utf8mb4&loc=Local", brand)
  16. case "ecco":
  17. url = fmt.Sprintf("root:plmPLM0.@tcp(%s.imix7.com:13306)/weimob?charset=utf8mb4&loc=Local", brand)
  18. }
  19. if len(url) == 0 {
  20. panic("mysql brand error")
  21. }
  22. db, err := sql.Open("mysql", url)
  23. if err != nil {
  24. panic(err)
  25. }
  26. err = db.Ping()
  27. if err != nil {
  28. panic(err)
  29. }
  30. mysqlDb = db
  31. }
  32. type Vip struct {
  33. Wid int64
  34. Phone string
  35. Name string
  36. }
  37. func GetAllWid() ([]Vip, error) {
  38. sqlStr := `select wid,phone,name from user order by wid `
  39. rows, err := mysqlDb.Query(sqlStr)
  40. if err != nil {
  41. return nil, err
  42. }
  43. defer rows.Close()
  44. var vips []Vip
  45. for rows.Next() {
  46. var vip Vip
  47. err := rows.Scan(&vip.Wid, &vip.Phone, &vip.Name)
  48. if err != nil {
  49. return nil, err
  50. }
  51. vips = append(vips, vip)
  52. }
  53. return vips, nil
  54. }