连接MySQL使用GoLang时失败

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

Connection fails with mysql using GoLang

问题

我正在尝试使用Go语言连接到MySQL数据库,并出现以下错误。

sql: unknown driver "mysql" (forgotten import?)

我的代码如下:

package main

import (
    "database/sql"
    "fmt"
)

func main() {
    db, err := sql.Open("mysql", "astaxie:astaxie@/test?charset=utf8")
    checkErr(err)
    err = db.Ping()
}

当我导入以下代码时:

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

我遇到了以下错误:

imported and not used
英文:

I am trying to connect the MySql DB using Go Language and gives me following error.

sql: unknown driver "mysql" (forgotten import?)

My Code

package main

    import (
        "database/sql"
        "fmt"
    )

func main() {
    db, err := sql.Open("mysql", "astaxie:astaxie@/test?charset=utf8")
    checkErr(err);
    err=db.Ping();
}

Also when I import

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

I am getting error of

imported and not used

答案1

得分: 43

对于因错误_sql: unknown driver "mysql"(忘记导入?)而来到此页面的其他人,必须使用database/sql包与数据库驱动程序一起使用。这意味着除了导入database/sql包之外,您还需要导入一个数据库驱动程序

例如,对于mysql,您可以使用go-sql-driver包。通常,您可以使用下划线_符号导入此包,这意味着它仅用于其副作用:

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

您可以在下面阅读更多相关信息并找到一些SQL驱动程序的列表:

英文:

For others coming to this page as a result of the error sql: unknown driver "mysql" (forgotten import?), the database/sql package must be used in conjunction with a database driver. That means in addition to importing the database/sql package, you need to import a database driver.

For example, for mysql, you could use the package go-sql-driver. Typically, you import this package using the underscore _ notation, meaning it is imported for its side effects only:

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

You can read more about this and find a list of SQL drivers below:

答案2

得分: 11

请再试一次,但是注意我的注释:

package main

import (
    "database/sql"

    _ "github.com/go-sql-driver/mysql"
)
// 注意 - 我删除了 "fmt" 的导入,因为它没有被使用。

func main() {
    db, err := sql.Open("mysql", "astaxie:astaxie@/test?charset=utf8")
    checkErr(err)
    err = db.Ping()
    // 注意 - 上面的代码会触发一个错误,因为 err 没有被使用。
}

我添加了 MySQL 驱动的导入,并删除了 "fmt",因为它没有被使用。这可能是你的 "imported and not used" 错误的原因。

英文:

Try it again, but look for my NOTEs:

package main

import (
    "database/sql"

    _ "github.com/go-sql-driver/mysql"
)
// NOTE - I removed the import for "fmt" because it was unused.

func main() {
    db, err := sql.Open("mysql", "astaxie:astaxie@/test?charset=utf8")
    checkErr(err);
    err=db.Ping();
    // NOTE - the above line will trigger an error because err is unused.
}

I added the import for the MySQL driver and removed "fmt" because it was unused. This may be the cause of your "imported and not used" error.

答案3

得分: 0

请尝试重新检查包的位置。当我手动将此包添加到项目中时,我犯了这样的错误。最好从GOROOT和GOPATH中清除此包,并按照源代码中指示的方式重新安装/连接它。链接如下:
https://github.com/go-sql-driver/mysql

英文:

Try to recheck the location of the packages. I made such a mistake when I manually added this package to the project. It is best to clean GOROOT and GOPATH from this package and reinstall/reconnect it as indicated in the source:
https://github.com/go-sql-driver/mysql

huangapple
  • 本文由 发表于 2016年3月28日 12:59:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/36256230.html
匿名

发表评论

匿名网友

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

确定