-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Sql export #1422
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
Sql export #1422
Conversation
…xtension. (Implements feature request sqlitebrowser#659)
…ser standards - Add "history" when closing editor window, but reopen before closing preferences - Revert some changes done by QtCreator
- [CHG] Always add "All files (*)" to filters - [FIX] Removed unused include
…per facilitarne l'import
Thanks @GortiZ6, this sounds interesting. 😄 Hopefully one of our C++ developers has time to review this in the next few days. 😄 |
Thanks @GortiZ6. The functionality will be useful and the implementation seems correct. I only have two concerns:
|
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.
Thanks, @GortiZ6! This is looking pretty good 👍
Just two minor points from my side. Both should be easy to fix 😄
But I agree with Manuel that we need to think about HTML and BLOBs. I haven't put much thought into this yet but would guess that it's best to not attach the HTML version when inSQL
is true because the user explicitly selected the SQL option here and this is what we should do then. And I would suggest using the X'...'
notation for BLOBs despite the fact that they won't work on all DBMSs -- unlike BASE64 text the X'...'
notation would at least produce an error.
src/ExtendedTableWidget.cpp
Outdated
@@ -314,35 +322,42 @@ void ExtendedTableWidget::copy(const bool withHeaders) | |||
const QString fieldSepText = "\t"; | |||
const QString rowSepText = "\r\n"; | |||
|
|||
QString sqlInsertStatement = QString("INSERT INTO \"%1\" ( '").arg(m->currentTableName().toString()); |
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.
Can you use sqlb::escapeIdentifier()
here for quoting the table name? This way we have one function which does the quoting which makes changing the quotes relatively easy (somewhere we have an issue for this).
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.
Done
result.append(rowSepText); | ||
htmlResult.append(rowSepHtml); | ||
sqlResult.append("\");\r\n"+sqlInsertStatement); |
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.
Not sure if \r\n
is the correct thing here. Because it's not CSV or something like that it might be better to use \r\n
only on Windows and \n
for all other platforms.
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.
Mhmm do you have any mean do discriminate which line termination to use or should I write an #ifdef __WIN32 #else #endif block?
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.
In fact aren't C/C++ compilers converting \n
to \r\n
on Windows platforms? We are assumming that \n
would produce a usual new line in several places. I would be surprised to know that those lines are not working under Windows.
[CHG] Using sqlb::escapeIdentifier() for table quotation to uniform with the rest of the code
Ok, I've made the two changes, I agree that the HTML is misleading so for now I simply omit it, we'll figure out if in the future someone comes up with a better idea. In the mean time I was also checking if there's a way to export a complete table in SQL, as far as I can see you can export the complete database in SQL or a table in CSV/JSON format, so I'm planning to add that as well to this merge request if that's ok for you. |
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.
All identifiers should use sqlb::escapeIdentifier. After PR #1436 users will be able to choose the quoting characters they prefer (from the set supported by SQLIte).
} | ||
|
||
result.append(escapeCopiedData(headerText)); | ||
htmlResult.append(headerText); | ||
sqlInsertStatement.append(escapeCopiedData(headerText)); |
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.
sqlb::escapeIdentifier
should be used here too for column names instead of escapeCopiedData
, and the ' single quotes' removed.
} | ||
result.append(rowSepText); | ||
htmlResult.append("</th></tr>"); | ||
sqlInsertStatement.append("') VALUES (\""); |
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.
Values should use ' single quotes' as required by SQL standard.
In the export dialog (File -> Export -> Database to SQL File...) you can select the tables to export. In the Database Structure you can drag and drop a row from a table to get the create and insert statements to recreate the table. The same from the Schema column of the DB Schema dock. These features are somewhat hidden, but take a look to them, just in case they fulfil your need or not. |
@mgrojo yes, I saw them (that's one of the reason I did not progress on this task, I was thinking about merging the two, making them less "hidden" and more homogenous with the rest of the exports or just dropping my idea). |
@GortiZ6 I will be making changes to this part of the code, so I think I will merge this to avoid conflicts and then I will fix the minor issues that are still pending. Thanks again for the pull request. |
- Set the line terminator according to operating system (text and SQL versions). - Quote identifiers using the user preference and string literals as 'SQL standard'. - Support BLOB literals for any kind of binary data and avoid converting any kind of image to PNG in the process of setting the BLOB literal in SQL.
I've added the ability to copy fields from tables in SQL format.
I found much robust and easier copy data in SQL from one DB to another so that if the number of columns differs or if I want to build a single SQL file to share several fields from several table with someone I can do it.