mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 11:00:44 -06:00
Store the data of a DB cell in a QByteArray, i.e. a binary data type, instead of putting it in a QString, thus converting it to a UTF8 string. Rewrite the reading and writing of DB cells to correctly handle binary data containing 0x00 bytes. Change the edit dialog to actually do all the data checks etc. on a currently invisible QHexEdit widget instead of a QTextEdit. All these changes combined make it possible to actually store binary data without it being corrupted. You can for example import pictures now, export them and actually open the exported file. So this is an improvement.
97 lines
3.1 KiB
C++
97 lines
3.1 KiB
C++
#include <QFile>
|
|
#include <QTextStream>
|
|
#include <QMessageBox>
|
|
#include <QFileDialog>
|
|
#include "ExportCsvDialog.h"
|
|
#include "ui_ExportCsvDialog.h"
|
|
#include "sqlitedb.h"
|
|
|
|
ExportCsvDialog::ExportCsvDialog(DBBrowserDB* db, const QString& deflocation, QWidget* parent)
|
|
: QDialog(parent),
|
|
ui(new Ui::ExportCsvDialog),
|
|
pdb(db),
|
|
defaultLocation(deflocation)
|
|
{
|
|
// Create UI
|
|
ui->setupUi(this);
|
|
|
|
// Get list of tables to export
|
|
objectMap objects = pdb->getBrowsableObjects();
|
|
for(objectMap::ConstIterator i=objects.begin();i!=objects.end();++i)
|
|
ui->comboTable->addItem(QIcon(QString(":icons/%1").arg(i.value().gettype())), i.value().getname());
|
|
}
|
|
|
|
ExportCsvDialog::~ExportCsvDialog()
|
|
{
|
|
delete ui;
|
|
}
|
|
|
|
void ExportCsvDialog::accept()
|
|
{
|
|
// Get filename
|
|
QString fileName = QFileDialog::getSaveFileName(
|
|
this,
|
|
tr("Choose a filename to export data"),
|
|
defaultLocation,
|
|
tr("Text files(*.csv *.txt)"));
|
|
|
|
// Only if the user hasn't clicked the cancel button
|
|
if(fileName.size() > 0)
|
|
{
|
|
// Get data from selected table
|
|
pdb->browseTable(ui->comboTable->currentText());
|
|
|
|
// Prepare the quote and separating characters
|
|
QString quoteChar = ui->comboQuoteCharacter->currentText();
|
|
QString quotequoteChar = quoteChar + quoteChar;
|
|
QString sepChar = ui->comboFieldSeparator->currentText();
|
|
if(sepChar == tr("Tab")) sepChar = "\t";
|
|
QString newlineChar = "\n";
|
|
|
|
// Open file
|
|
QFile file(fileName);
|
|
if(file.open(QIODevice::WriteOnly))
|
|
{
|
|
// Open text stream to the file
|
|
QTextStream stream(&file);
|
|
|
|
// Put field names in first row if user wants to have them
|
|
if(ui->checkHeader->isChecked())
|
|
{
|
|
QStringList fields = pdb->browseFields;
|
|
for(int i=0;i<fields.count();i++)
|
|
{
|
|
stream << quoteChar << fields.at(i) << quoteChar;
|
|
if(i < fields.count() - 1)
|
|
stream << sepChar;
|
|
else
|
|
stream << newlineChar;
|
|
}
|
|
}
|
|
|
|
// Get and write actual data
|
|
rowList data = pdb->browseRecs;
|
|
for(int i=0;i<data.size();i++)
|
|
{
|
|
QList<QByteArray> row = data[i];
|
|
for(int j=1;j<row.size();j++)
|
|
{
|
|
QString content = row[j];
|
|
stream << quoteChar << content.replace(quoteChar, quotequoteChar) << quoteChar;
|
|
if(j < row.count() - 1)
|
|
stream << sepChar;
|
|
else
|
|
stream << newlineChar;
|
|
}
|
|
}
|
|
|
|
// Done writing the file
|
|
file.close();
|
|
QMessageBox::information(this, QApplication::applicationName(), tr("Export completed."));
|
|
QDialog::accept();
|
|
} else {
|
|
QMessageBox::warning(this, QApplication::applicationName(), tr("Could not open output file."));
|
|
}
|
|
}
|
|
}
|