douhan8892 2018-05-11 23:34
浏览 128
已采纳

Go数据库连接器:go-sql-driver可以工作,其他所有“未知的驱动程序,忘记了导入?”

When I try to use database/sql in this way it compiles and works:

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

But if I try to use postgres specific connectors it doesn't even compile:

import(
    "database/sql"
    _ "github.com/lib/pq"
)

import(
    "database/sql"
    _ "github.com/jbarham/gopgsqldriver"
)

both fail with the error

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

I have done go get for both of these packages, and am really not sure why it is not compiling

  • 写回答

1条回答 默认 最新

  • dongying9712 2018-05-12 00:04
    关注

    Are you doing

    db, err := sql.Open("mysql",
    

    later on? When you import "github.com/lib/pq" for example, it registers itself by calling sql.Register, and then in the source of sql.Open you have:

    func Open(driverName, dataSourceName string) (*DB, error) {
        driversMu.RLock()
        driveri, ok := drivers[driverName]
        driversMu.RUnlock()
        if !ok {
            return nil, fmt.Errorf("sql: unknown driver %q (forgotten import?)", driverName)
        }
    }
    

    So, since you are no longer importing mysql, you need to change sql.Open to use the pq driver (or whichever one you end up picking).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序