-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Import CSV: Add option to insert missing values as NULL #1349
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
Import CSV: Add option to insert missing values as NULL #1349
Conversation
| </item> | ||
| <item row="7" column="0"> | ||
| <layout class="QVBoxLayout" name="verticalLayout_2"/> | ||
| </item> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to have been there by mistake
|
Excellent @remram44, this should move things in the right direction. 😄 @mgrojo or @MKleusberg Would either of you be ok to do a quick review of this code? It looks pretty simple. @remram44 As a thought, it's probably useful to also add the "use default value for the field, if defined" (possibly with better wording) thing too. I'm kind of expecting that'd be well used as well, though that's more a gut feeling thing than based on any real evidence. 😉 |
|
From what I understand, the default is added if you don't specify the field at all in the statement, so there would need to be more than one prepared statement to do this. Therefore it is more complex than the case I already added. |
|
Thanks for the contribution, @remram44. I've made a quick review. It's already a useful addition but ideally it should also work for non-existing tables. I think it is right to leave the current behaviour for integer columns. It makes no sense to insert there an empty string. I have a doubt about general NOT NULL columns of existing tables when the insert-null option is selected. Should the same check as for integer columns performed and an empty-string be inserted instead? Or should it fail the import? I'd think that it is better to insert the empty-string for that column but allow to insert the NULL for others. With the current PR it would fail (not tested, just assuming it). |
No worries, it was just a thought. 😄 |
That's a good point. I'm not sure what the ideal behaviour should be, but we should at least warn the user somehow when this situation occurs so they can take some kind of action (eg make a decision, fix things manually, etc). |
|
I personally like the option of having it fail if my data has unexpected NULLs, or if my table has an unexpected NOT NULL. It's easy enough to select a different option in the import or remove that NOT NULL constraint, and I definitely wouldn't want things to happen silently. I can actually see myself selecting the "import as NULL" option to import data into a NOT NULL column when I don't expect missing values. |
Seems like I completely skipped over that |
|
@remram44 Yeah, failing the import for that situation makes sense. 😄 |
|
The situation when creating a new table is somewhat weird, e3e2e81 is my attempt. It will insert NULLs if that's what was selected, and otherwise will stick to the current behavior; however I'm not sure about that behavior. |
|
This is looking pretty good! Thanks a lot, @remram44 👍 I have stumbled upon two unexpected peculiarities:
So, as said I would leave it this way and merge this as-is. I certainly couldn't find any problems in the actual implementation 😄 |
|
As for the debate regarding default values: We probably don't need different prepared statements here when we use the Either way, if you want to and have the time you can add this as a third combo box option, @remram44. If not, I will give it a try 😄 |
|
I think I will let you work on the third option @MKleusberg, as I'm not sure I will be able to get to it for a while. |
|
I just want to say something. 😃 Regarding the expected behavior of DB4S when importing empty values from a CSV.
|
|
That actually makes a lot of sense. 😺 |
|
Maybe I'm overthinking this but what about the case where I want to import a CSV file into my table and keep it exactly as it is (i.e. empty fields not replaced by default values) to then do something with these empty records? Trying to come up with an example: lets say I have a table What do you think? Maybe I'm missing something? |
|
Yes, you might be overthinking this. 😄 I believe the database design should be honored over the imported file. The default value should be inserted whenever no value is given, and that's how SQLite works. If you want to import some data into a column with default value but do not want it to be set for empty values (violating the database design), then you shouldn't have a default value. For your example, you can remove the default value, import the csv, then set it again after import to get the result you are after. If it is not user-friendly, or you might forget to do that every time before importing, then a check mark "ignore/don't set default values for empty fields" (with a tool tip explaining that the default behavior is to set the default value when none is given) should be enough to do this automatically (unset default value, import success, restore back). One thing that might be a problem though is finding out what the user thinks is a Is it User A might think that both are |
|
You're probably right, @karim. I think I might add the checkbox as you have suggested anyway, just to make sure we lose no functionality. And it should be easy to add 😉 If it turns out we would need more checkboxes to cover some other settings I will hide them away behind an 'Advanced' button. The But that's definitely beyond this pull request. So I will merge this now and deal with the default settings later because this is already a huge improvement 😄 Thanks again, @remram44, and sorry for taking so much time merging this 😉 If you ever feel like making another pull request you're very welcomed to do so 😄 |
|
No problem! Sorry I couldn't take care of the rest of the concerns here, |
|
No worries 😄 I just opened another issue so they aren't lost and will deal with them over the next couple of days. And either way, your pull request did most of the work here already. |
* Add a combo box for missing values in import ui * Add ui pieces to .cpp file * Insert NULL values if requested * Allow inserting NULLs in new table also
This adds a combo box to select how missing values should be inserted.
Fixes #1339
Current behavior:
sqlitebrowser/src/ImportCsvDialog.cpp
Lines 560 to 565 in a242a58
The options after my patch are "empty string" (default, matches sqlite3's
.import, current behavior) or "NULL" (probably what you want).Open questions:
INTEGERcolumns, which is NULL if NULL is allowed, 0 otherwise0/false/"(missing)"/...)