英文:
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)
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论