I'm trying to INSERT data into POSTGRES from a .csv (pre-fixed width / tabular ) with GO.
What I've done:
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)
}
when I run, I get this error
[{"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
You imported the sql/database
, a package contains generic interface for sql-related operation.
Since it's only generic interface, you need to import the concrete implementation of the interface, and in this context, it's the database driver.
From your code: sql.Open("postgres", psqlInfo)
, I presume you are using postgresql database. There are some postgresql drivers for golang available, one of them is https://github.com/lib/pq driver. So add it to the import statement.
package main
import (
"bufio"
"database/sql"
"encoding/csv"
"encoding/json"
"fmt"
"io"
"log"
"os"
_ "github.com/lib/pq" // <------------ here
)
The database driver pq
must be imported with _
character in front of the import statement. It's because we don't use it explicitly on the code meanwhile it's still required by database/sql
package. For more details see this related SO question What does an underscore in front of an import statement mean?.
More information about golang sql: https://pkg.go.dev/database/sql. .
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With