使用MySQL数据库进行Revel开发,但不使用GORM。

huangapple go评论115阅读模式
英文:

using mysql db for revel without gorm

问题

我已经将所有内容放入了app.go文件中,数据库正确打开,但是Index函数无法访问全局变量。全局变量似乎不是全局的,因为如果我在InitDB函数内部分配完后删除对Db的使用,就会出现"Db declared and not used"的错误。

  1. package controllers
  2. import (
  3. "database/sql"
  4. "fmt"
  5. _ "github.com/go-sql-driver/mysql"
  6. "github.com/revel/revel"
  7. )
  8. var Db *sql.DB
  9. type App struct {
  10. *revel.Controller
  11. }
  12. func (c App) Index() revel.Result {
  13. if c.Params.Get("id") == "3012" {
  14. return c.Redirect("http://youtube.com")
  15. }
  16. fmt.Println("here is the db from index:", Db)
  17. return c.Render()
  18. }
  19. func InitDB() {
  20. // open db
  21. Db, err := sql.Open("mysql", "username:xxxxxxx@tcp(xxxxxxx:3306)/xxxx")
  22. if err != nil {
  23. revel.INFO.Println("DB Error", err)
  24. }
  25. revel.INFO.Println("DB Connected")
  26. //fmt.Println(Db)
  27. }
  28. func init() {
  29. revel.OnAppStart(InitDB)
  30. }

希望能帮到你!谢谢。

英文:

I've placed everything into app.go and the database opens correctly but Index cannot access the global variable. The global variable doesn't seem to be global because if I remove the use of Db after assigning it inside InitDB I get the error "Db declared and not used"

  1. package controllers
  2. import (
  3. "database/sql"
  4. "fmt"
  5. _ "github.com/go-sql-driver/mysql"
  6. "github.com/revel/revel"
  7. )
  8. var Db *sql.DB
  9. type App struct {
  10. *revel.Controller
  11. }
  12. func (c App) Index() revel.Result {
  13. if c.Params.Get("id") == "3012" {
  14. return c.Redirect("http://youtube.com")
  15. }
  16. fmt.Println("here is the db from index:", Db)
  17. return c.Render()
  18. }
  19. func InitDB() {
  20. // open db
  21. Db, err := sql.Open("mysql", "username:xxxxxxx@tcp(xxxxxxx:3306)/xxxx")
  22. if err != nil {
  23. revel.INFO.Println("DB Error", err)
  24. }
  25. revel.INFO.Println("DB Connected")
  26. //fmt.Println(Db)
  27. }
  28. func init() {
  29. revel.OnAppStart(InitDB)
  30. }

any help would be appreciated! Thanks.

答案1

得分: 1

你正在错误地声明变量。你当前的方式就像在InitDB作用域中声明一个普通变量一样(你使用了:=)。

  1. func InitDB() {
  2. var err error
  3. Db, err = sql.Open("mysql", "username:xxxxxxx@tcp(xxxxxxx:3306)/xxxx")
  4. if err != nil {
  5. revel.INFO.Println("DB Error", err)
  6. }
  7. revel.INFO.Println("DB Connected")
  8. //fmt.Println(Db)
  9. }

我还建议在app文件夹内创建一个名为controllers的文件夹,用于存储所有的路由逻辑,并在需要时调用app.Db变量。

这样应该可以工作。

英文:

You are declaring your variable in a wrong way. The current way you are using its like declaring a normal variable for the InitDB scope (you are using := ).

  1. func InitDB() {
  2. var err error
  3. Db, err = sql.Open("mysql", "username:xxxxxxx@tcp(xxxxxxx:3306)/xxxx")
  4. if err != nil {
  5. revel.INFO.Println("DB Error", err)
  6. }
  7. revel.INFO.Println("DB Connected")
  8. //fmt.Println(Db)
  9. }

I also suggest creating a folder inside app called controllers to store all your route logic and if needed call the app.Db variable

This should work

huangapple
  • 本文由 发表于 2016年9月11日 08:30:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/39431794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定