如何解决«panic:sql:unknown driver“postgres”(forgotten import?

mtb9vblg  于 10个月前  发布在  Go
关注(0)|答案(1)|浏览(213)

我正尝试使用GO将数据从.csv(前缀固定宽度/表格)转换为POSTGRES。
我所做的:

package main

import (
    "bufio"
    "database/sql"
    "encoding/csv"
    "encoding/json"
    "fmt"
    "io"
    "log"
    "os"
)

type Consumidor struct {
    CPF string   `json:"CPF"`
    Private  string   `json:"Private"`
    Incompleto  string   `json:"Incompleto"`
    Compras   *Compras `json:"Compras,omitempty"`
}

type Compras struct {
    DataUltimacompra  string `json:"DataUltimacompra"`
    TicketMedio string `json:"TicketMedio"`
    TicketUltimaCompra string `json:"TicketUltimaCompra"`
    LojaMaisFrequente string `json:"LojaMaisFrequente"`
    LojaUltimaCompra string `json:"LojaUltimaCompra"`
}

const (
    host     = "localhost"
    port     = 5432
    user     = "postgres"
    password = ""
    dbname   = "neoway"
)

func main() {
    csvFile, _ := os.Open("data.csv")
    reader := csv.NewReader(bufio.NewReader(csvFile))
    var dadosinsert []Consumidor
    for {
        line, error := reader.Read()
        if error == io.EOF {
            break
        } else if error != nil {
            log.Fatal(error)
        }
        dadosinsert = append(dadosinsert, Consumidor{
            CPF: line[0],
            Private:  line[1],
            Incompleto: line[2],
            Compras: &Compras{
                DataUltimacompra:  line[3],
                TicketMedio:  line[4],
                TicketUltimaCompra: line[5],
                LojaMaisFrequente:  line[6],
                LojaUltimaCompra: line[7],

            },
        })
    }
    peopleJson, _ := json.Marshal(dadosinsert)
    fmt.Println(string(peopleJson))

    psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
        "password=%s dbname=%s sslmode=disable",
        host, port, user, password, dbname)
    db, err := sql.Open("postgres", psqlInfo)
    if err != nil {
        panic(err)
    }
    defer db.Close()

    sqlStatement := `
INSERT INTO base_teste (CPF,"PRIVATE","INCOMPLETO","DATA DA ÚLTIMA COMPRA","TICKET MÉDIO","TICKET DA ÚLTIMA COMPRA","LOJA MAIS FREQUÊNTE","LOJA DA ÚLTIMA COMPRA")
)
VALUES ($1, $2, $3, $4, $5, $6, 7$, 8$)
RETURNING id`
    id := 0
    err = db.QueryRow(sqlStatement, 30, "a", "b", "c").Scan(&id)
    if err != nil {
        panic(err)
    }
    fmt.Println("New record ID is:", id)
}

字符串
当我运行时,我得到这个错误
[{“CPF”:“xxxxx”,“Private”:“TRUE”,“Incompleto”:“不完整”,“Compras”:{“DataUltimacompra”:“12/10/2018”,“TicketMedio”:“200”,“TicketUltimaCompra”:“250”,“LojaMaisFrequente”:“111.111.111-99”,“LojaUltimaCompra”:“111.111.111-88”}}] panic:sql:unknown driver“postgres”(forgotten import?)
goroutine 1 [running]:main.main()C:/Users/Willian/Desktop/NEOWAY PROJECT/neoway csv prefixed width importer/main.go:70 +0xbed
进程已完成,退出代码为2

lmvvr0a8

lmvvr0a81#

你导入了sql/database,一个包含通用接口的包,用于sql相关的操作。
因为它只是一个 * 通用接口 *,所以您需要导入接口的具体实现,在此上下文中,它是数据库驱动程序。
从你的代码:sql.Open("postgres", psqlInfo),我假设你使用的是postgresql数据库。有一些postgresql drivers for golang可用,其中一个是https://github.com/lib/pq驱动程序。所以将其添加到import语句中。

package main

import (
    "bufio"
    "database/sql"
    "encoding/csv"
    "encoding/json"
    "fmt"
    "io"
    "log"
    "os"
    _ "github.com/lib/pq" // <------------ here
)

字符串
数据库驱动程序pq必须在导入语句前使用_字符导入。这是因为我们没有在代码中显式使用它,同时database/sql包仍然需要它。有关详细信息,请参阅此相关SO问题What does an underscore in front of an import statement mean?
关于golang sql:https://pkg.go.dev/database/sql的更多信息。

相关问题