Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Insert rewrites valid escaped entries wrong #106

Open
derphilipp opened this issue May 2, 2023 · 2 comments
Open

Insert rewrites valid escaped entries wrong #106

derphilipp opened this issue May 2, 2023 · 2 comments

Comments

@derphilipp
Copy link

Situation: We have a valid file, with newlines in a string, we can query:

file:

ID,Username,Text,Counter
1,Laura Bow,"What a nice 

newline",0

query:

csvq 'SHOW FIELDS FROM `new.csv`'

results in


                  Fields in new.csv
-----------------------------------------------------
 Type: File
 Path: /Users/phw/dev/csvq/new.csv
 Format: CSV     Delimiter: ','   Enclose All: false
 Encoding: UTF8  LineBreak: LF    Header: true
 Status: Fixed
 Fields:
   1. ID
   2. Username
   3. Text
   4. Counter

also

csvq 'SELECT * FROM `new.csv`'

results in

ID,Username,Text,Counter
1,Laura Bow,What a nice 

newline,0

But if we now enter a new entry:

csvq -k 'INSERT into `new.csv` (ID, Username, Text, Counter) VALUES(2,\'Gloria\',\'sometext\', 0);'

it broke our existing file! (note the missing quotes in the "What a nice ..... newline" line)

ID,Username,Text,Counter
1,Laura Bow,What a nice 

newline,0
2,Gloria,sometext,0

This happens in the database-driver version as well as in the command line client.
Am I missing a parameter / using it wrong?

Thanks for your awesome work!

@mithrandie
Copy link
Owner

mithrandie commented May 3, 2023

It's a bug.

Csvq automatically encloses a field when delimiters or enclosures are present in the field data. However, if a field contains newline characters, the field is not enclosed.

As a workaround, you can enclose all fields by setting the "Enclose All" attribute of the table to "true" before updating the data.

csvq 'ALTER TABLE `new.csv` SET ENCLOSE_ALL TO TRUE; INSERT into `new.csv` (ID, Username, Text, Counter) VALUES(2, "Gloria", "sometext", 0);'

@derphilipp
Copy link
Author

Thank you very much, that workaround really helps a ton!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants