DB640/tools/csvimport/main.go

37 lines
999 B
Go
Raw Normal View History

2020-03-23 20:08:30 +00:00
package main
import (
"flag"
"log"
"git.1750studios.com/ToddShepard/DB640/internal/csvparser"
"git.1750studios.com/ToddShepard/DB640/internal/database"
)
func main() {
input := flag.String("i", "", "Specifies CSV input file")
dialect := flag.String("d", "sqlite3", "Dialect for the database connection")
connection := flag.String("c", "./csvfile.db", "Connection string for the database connection")
flag.Parse()
if *input == "" {
log.Fatalf("Please specify the input file!")
}
err := database.Open(*dialect, *connection)
if err != nil {
log.Fatalf("Could not connect to database, error: %+v", err)
}
lines, err := csvparser.ParseFile(*input)
if err != nil {
log.Fatalf("Could not parse file, error: %+v", err)
}
2020-04-02 17:49:56 +00:00
for i, line := range lines {
if i == 0 {
//Ignore header
continue
}
2020-03-23 20:08:30 +00:00
database.Db.Create(&database.Betriebsstelle{Code: line.Code, Name: line.Name})
log.Printf("Inserted %s with name %s into database!", line.Code, line.Name)
}
database.Close()
}