Original post

Hey,

I’ve written some code that unmarshals data from a .csv file and then creates inserts based on the data within the csv file.

The code Im working with has one issue when scanning through the .csv file: It reads the header line and tries to insert those fields.

How I can skip the header line so that I can then read the actual data and parse said data into my DB?

func InsertTestFileLoader(filename string, channel chan InsertTestThing) { defer close(channel) file, err := os.Open(filename) if err != nil { panic(err) } defer file.Close() var InsertTestNew InsertTestThing reader := csv.NewReader(file) for { err := unmarshal.Unmarshal(reader, &InsertTestNew) if err == io.EOF { break } if err != nil { panic(err) } channel <- InsertTestNew } } func saveInsertTest(channel <-chan InsertTestThing, db *sql.DB) { stmt, err := db.Prepare(`INSERT INTO table1 (id, customer_id, name) VALUES (?, ?, ?);`) if err != nil { log.Fatal(err) } for InsertTestThing := range channel { _, err := stmt.Exec( InsertTestThing.ID, InsertTestThing.CustomerID, InsertTestThing.Name, ) if err != nil { log.Fatal(err) } } } func InsertTestInsert() { db, _ := sql.Open( "mysql", "?") channel := make(chan InsertTestThing)  InsertTestFileLoader("InsertTest.csv", channel) saveInsertTest(channel, db) defer db.Close() } 

submitted by /u/squeedgys
[link] [comments]