1

I'm coding a simple tool to help myself at work but it's mostly for learning purposes, this tool takes in a ".csv" file and converts the data in the file to sql insert into statements, then write the statements to a ".sql" file. The problem is that after it's done, when I check the file, the last row is never complete, usually it stops

INSERT INTO tablename (field1, field2, field3, field4

I'm using bufio.NewWriter, and the function WriteString, and it's not returning any error, so I have no idea what's happening, the full ".csv" file I'm testing on is 8Kb, but I can't share it because it contains sensible info sorry.

Another problem I'm having is with this particular statement

sqlFile, err := os.Create(date.Format("20-11-2018 20.10.05") + ".sql")

What I try to do here is to put a timestamp as the name of the file with the format "dd-MM-yyyy hh.mm.ss" but the file names end up like this "200-1010-20108 200.100.34.sql", no idea what's happening here.

Before anyone tells me, yes I'm sure there are already tools for this, but a big part of the motivation to do this is to learn, so I welcome any idea or criticism to the entire code and not just the parts I'm having problems with.

Link to the github page

Thanks in advance!

ner0ck
  • 21
  • 3
  • 1
    Use [os.File.Sync](https://golang.org/pkg/os/#File.Sync) to commit the data to disk before closing the file. – mkopriva Oct 20 '18 at 20:17
  • 1
    https://stackoverflow.com/questions/25845172/parsing-date-string-in-golang/25845833#25845833 explains your second problem (although it's about Parse, same thing applies to Format) – mkopriva Oct 20 '18 at 20:20
  • @mkopriva just tried it and didn't make a difference, tho I had forgot to Close the files and at least now I'm closing them, but didn't fixe the problem. The naming problem was solved with your link, thanks, I'm giving this string now to Format(), "02-01-2006 15.04.05", but I'm not sure I understand how it makes a difference to what I had before – ner0ck Oct 20 '18 at 20:30

1 Answers1

4

Package bufio

import "bufio" 

func (*Writer) Flush

func (b *Writer) Flush() error

Flush writes any buffered data to the underlying io.Writer.

After you have finished writing, flush the buffered writer before closing the file.

peterSO
  • 158,998
  • 31
  • 281
  • 276