From 9f0eca04c6842ad4af5244b07f42cec242f9d3e0 Mon Sep 17 00:00:00 2001 From: prasad83 Date: Tue, 20 Feb 2024 18:38:01 +0530 Subject: [PATCH 1/2] Added separator config to support TSV files Use -s="\t" for TSV --- csv2sql.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/csv2sql.go b/csv2sql.go index c1ab391..13d13a5 100644 --- a/csv2sql.go +++ b/csv2sql.go @@ -48,6 +48,7 @@ var csvFileName string var keepOrigCols bool var debugSwitch bool var helpMe bool +var separatorVal string // init() function - always runs before main() - used here to set-up required flags variables // from the command line parameters provided by the user when they run the app @@ -56,6 +57,7 @@ func init() { // IntVar; StringVar; BoolVar all required: variable, cmd line flag, initial value, description used by flag.Usage() on error / help flag.StringVar(&tableName, "t", "", "\tUSE: '-t tablename' where tablename is the name of the SQLite table to hold your CSV file data [MANDATORY]") flag.StringVar(&csvFileName, "f", "", "\tUSE: '-f filename.csv' where filename.csv is the name and path to a CSV file that contains your data for conversion [MANDATORY]") + flag.StringVar(&separatorVal, "s", ",", "\tUSE: for tab '-s=\"\\t\" or '-s=\",\"' [OPTIONAL]") flag.BoolVar(&keepOrigCols, "k", false, "\tUSE: '-k=true' to keep original csv header fields as the SQL table column names") flag.BoolVar(&debugSwitch, "d", false, "\tUSE: '-d=true' to include additional debug output when run") flag.BoolVar(&helpMe, "h", false, "\tUSE: '-h' to provide more detailed help on using this program") @@ -227,6 +229,16 @@ func main() { // TODO : is there an error from this to check? reader := csv.NewReader(file) + // TSV support + if separatorVal != "" { + if debugSwitch { + fmt.Printf("Using separator: %s\n", separatorVal) + } + if separatorVal == "\\t" { + reader.Comma = '\t' + } + } + //------------------------------------------------------------------------- // open and prepare the SQL output file //------------------------------------------------------------------------- From c54efaa092c021fa15983f912afa6eed352b5c0f Mon Sep 17 00:00:00 2001 From: prasad83 Date: Tue, 20 Feb 2024 18:51:06 +0530 Subject: [PATCH 2/2] Fixed - escaping double quote when writing value to SQL. --- csv2sql.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/csv2sql.go b/csv2sql.go index 13d13a5..04d14fd 100644 --- a/csv2sql.go +++ b/csv2sql.go @@ -338,7 +338,7 @@ func main() { if len(record[i]) == 0 || record[i] == "NULL" { strbuffer.WriteString("NULL") } else { - strbuffer.WriteString("\"" + record[i] + "\"") + strbuffer.WriteString("\"" + strings.Replace(record[i], "\"", "\"\"", -1) + "\"") /* escape " with "" */ } // if we have not reached the last record yet - add a coma also to the output if i < len(record)-1 {