gpt4 book ai didi

postgresql - 我该如何解决 «panic : sql: unknown driver "postgres" (forgotten import? )»?

转载 作者:IT王子 更新时间:2023-10-29 01:25:00 31 4
gpt4 key购买 nike

我正在尝试使用 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":"FALSE","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

Process finished with exit code 2

最佳答案

你导入了sql/database,一个包含了sql相关操作的通用接口(interface)的包。

由于它只是通用接口(interface),您需要导入接口(interface)的具体实现,在这种情况下,它是数据库驱动程序。

根据您的代码:sql.Open("postgres", psqlInfo),我假设您正在使用 postgresql 数据库。有一些postgresql drivers for golang可用,其中之一是https://github.com/lib/pq司机。所以将它添加到导入语句中。

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 ..

关于postgresql - 我该如何解决 «panic : sql: unknown driver "postgres" (forgotten import? )»?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52789531/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com