使用Gorm时出现undefined: mysql.Open错误。

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

Using Gorm getting undefined: mysql.Open

问题

我已经查看了gorm的文档,并且我认为我正确地遵循了模式。我已经运行了go build和go mod tidy。

但是仍然出现相同的错误。

package main

import (
	"encoding/json"
	"github.com/go-sql-driver/mysql"
	"gorm.io/gorm"
	"net/http"
)

var DB *gorm.DB
var err error

const DNS = "root:654321cg@tcp(127.0.0.1:3306)/resourcesdb?charset=utf8&parseTime=True&loc=Local"

...

func InitialMigration() {
	DB, err = gorm.Open(mysql.Open(DNS), &gorm.Config{})
	if err != nil {
		println(err.Error())
		panic("Cannot connect to DB")
	}
	DB.AutoMigrate(&Client{})
}

func createClient(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	var client Client
	json.NewDecoder(r.Body).Decode(&client)
	DB.Create(&client)
	json.NewEncoder(w).Encode(client)
}

以上是你提供的代码。

英文:

reviewed the docs for gorm, and I think I am following the pattern correctly. I have run go build and go mod tidy.

but still the same error persists.

package main

import (
	"encoding/json"
	"github.com/go-sql-driver/mysql"
	"gorm.io/gorm"
	"net/http"
)

var DB *gorm.DB
var err error

const DNS = "root:654321cg@tcp(127.0.0.1:3306)/resourcesdb?charset=utf8&parseTime=True&loc=Local"

...

func InitialMigration()  {
	DB, err = gorm.Open(mysql.Open(DNS), &gorm.Config{})
	if err != nil {
		println(err.Error())
		panic("Cannot connect to DB")
	}
	DB.AutoMigrate(&Client{})
}



func createClient(w http.ResponseWriter, r *http.Request) {
	w.Header().Set("Content-Type", "application/json")
	var client Client
	json.NewDecoder(r.Body).Decode(&client)
	DB.Create(&client)
	json.NewEncoder(w).Encode(client)
}

答案1

得分: 0

我弄清楚了。导入了错误的包。

"gorm.io/driver/mysql"

替换为

"github.com/go-sql-driver/mysql"

可以解决这个问题。

英文:

figured this out. imported the wrong pkg.

substituting

"gorm.io/driver/mysql"

instead of

"github.com/go-sql-driver/mysql"

fixes it.

答案2

得分: 0

"github.com/go-sql-driver/mysql"替换为"gorm.io/driver/mysql"

import (
    "encoding/json"
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
    "net/http"
)

var DB *gorm.DB
var err error

const DNS = "root:654321cg@tcp(127.0.0.1:3306)/resourcesdb?charset=utf8&parseTime=True&loc=Local"

...

func InitialMigration()  {
    DB, err = gorm.Open(mysql.Open(DNS), &gorm.Config{})
    if err != nil {
        panic("无法连接到数据库")
    }
    DB.AutoMigrate(&Client{})
}



func createClient(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    var client Client
    json.NewDecoder(r.Body).Decode(&client)
    DB.Create(&client)
    json.NewEncoder(w).Encode(client)
}
英文:

remplace "github.com/go-sql-driver/mysql" with "gorm.io/driver/mysql"

import (
    "encoding/json"
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
    "net/http"
)

var DB *gorm.DB
var err error

const DNS = "root:654321cg@tcp(127.0.0.1:3306)/resourcesdb?charset=utf8&parseTime=True&loc=Local"

...

func InitialMigration()  {
    DB, err = gorm.Open(mysql.Open(DNS), &gorm.Config{})
    if err != nil {
        panic("Cannot connect to DB")
    }
    DB.AutoMigrate(&Client{})
}



func createClient(w http.ResponseWriter, r *http.Request) {
    w.Header().Set("Content-Type", "application/json")
    var client Client
    json.NewDecoder(r.Body).Decode(&client)
    DB.Create(&client)
    json.NewEncoder(w).Encode(client)
}

huangapple
  • 本文由 发表于 2021年8月28日 19:39:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/68963996.html
匿名

发表评论

匿名网友

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

确定