Example #1
0
File: main.go Project: tadvi/dbf
func save(dbffile, csvfile string) {
	db, err := dbf.LoadFile(dbffile)
	if err != nil {
		log.Fatal(err)
	}

	fl, err := os.Create(csvfile)
	if err != nil {
		log.Fatal(err)
	}
	defer fl.Close()
	w := csv.NewWriter(fl)
	defer w.Flush()

	header := []string{}
	for _, field := range db.Fields() {
		header = append(header, field.Name)
	}
	if err := w.Write(header); err != nil {
		log.Fatal(err)
	}

	// once we have both CSV and DBF open, write CSV while iterating rows
	var count int
	iter := db.NewIterator()
	for iter.Next() {
		arr := iter.Row()
		if err := w.Write(arr); err != nil {
			log.Fatal(err)
		}
		count++
	}
	log.Println("Total records in CSV:", count)
}
Example #2
0
File: main.go Project: tadvi/dbf
func main() {
	db := dbf.New()
	if err := db.Create(Note{}); err != nil {
		panic(err)
	}

	db.Append(Note{"Tom", 444})
	db.Append(Note{"Bob", 555})
	db.Append(Note{"Stan", 777})
	err := db.SaveFile("temp.dbf")
	if err != nil {
		panic(err)
	}

	db, err = dbf.LoadFile("temp.dbf")
	if err != nil {
		panic(err)
	}

	iter1 := db.NewIterator()
	for iter1.Next() {
		n := new(Note)
		iter1.Read(n)
		n.Amount = 12
		iter1.Write(n)
		fmt.Println(*n)
	}

	iter := db.NewIterator()
	for iter.Next() {
		n := new(Note)
		iter.Read(n)
		fmt.Println(*n)
	}

	err = db.SaveFile("temp.dbf")
	if err != nil {
		panic(err)
	}
}