mirror of
https://github.com/sqlitebrowser/sqlitebrowser.git
synced 2026-01-20 11:00:44 -06:00
SqliteTableModel: Make editing of data possible again
This commit is contained in:
@@ -104,7 +104,7 @@ void MainWindow::init()
|
||||
connect(ui->dataTable->horizontalHeader(), SIGNAL(sectionClicked(int)), this, SLOT(browseTableHeaderClicked(int)));
|
||||
connect(ui->dataTable->verticalScrollBar(), SIGNAL(valueChanged(int)), this, SLOT(setRecordsetLabel()));
|
||||
connect(editWin, SIGNAL(goingAway()), this, SLOT(editWinAway()));
|
||||
connect(editWin, SIGNAL(updateRecordText(int, int, QByteArray)), this, SLOT(updateRecordText(int, int , QByteArray)));
|
||||
connect(editWin, SIGNAL(updateRecordText(int, int, QByteArray)), this, SLOT(updateRecordText(int, int, QByteArray)));
|
||||
|
||||
// Load window settings
|
||||
restoreGeometry(PreferencesDialog::getSettingsValue("MainWindow", "geometry").toByteArray());
|
||||
@@ -277,7 +277,7 @@ void MainWindow::populateTable( const QString & tablename, bool keepColumnWidths
|
||||
{
|
||||
QApplication::setOverrideCursor( Qt::WaitCursor );
|
||||
if(!tablename.isEmpty())
|
||||
m_browseTableModel->setQuery(QString("SELECT * FROM `%1`").arg(tablename));
|
||||
m_browseTableModel->setTable(tablename);
|
||||
// bool mustreset = false;
|
||||
// if (tablename.compare(db.curBrowseTableName)!=0)
|
||||
// {
|
||||
@@ -706,18 +706,7 @@ void MainWindow::helpAbout()
|
||||
|
||||
void MainWindow::updateRecordText(int row, int col, const QByteArray& newtext)
|
||||
{
|
||||
if (!db.updateRecord(row, col, newtext)){
|
||||
QMessageBox::information( this, QApplication::applicationName(),
|
||||
tr("Data could not be updated:\n") + db.lastErrorMessage);
|
||||
}
|
||||
|
||||
rowList tab = db.browseRecs;
|
||||
QList<QByteArray>& rt = tab[row];
|
||||
QByteArray& cv = rt[col+1];//must account for rowid
|
||||
|
||||
QStandardItem* item = new QStandardItem(QString(cv));
|
||||
item->setToolTip( wrapText(cv) );
|
||||
browseTableModel->setItem(row, col, item);
|
||||
m_browseTableModel->setData(m_browseTableSortProxy->mapToSource(m_browseTableSortProxy->index(row, col)), newtext);
|
||||
}
|
||||
|
||||
void MainWindow::editWinAway()
|
||||
@@ -727,13 +716,9 @@ void MainWindow::editWinAway()
|
||||
ui->dataTable->setCurrentIndex(ui->dataTable->currentIndex().sibling(editWin->getCurrentRow(), editWin->getCurrentCol()));
|
||||
}
|
||||
|
||||
void MainWindow::editText(int row, int col)
|
||||
void MainWindow::editText(const QModelIndex& index)
|
||||
{
|
||||
rowList tab = db.browseRecs;
|
||||
QList<QByteArray> rt = tab[row];
|
||||
QByteArray cv = rt[col+1];//must account for rowid
|
||||
|
||||
editWin->loadText(cv , row, col);
|
||||
editWin->loadText(index.data().toByteArray(), index.row(), index.column());
|
||||
editWin->show();
|
||||
}
|
||||
|
||||
@@ -749,7 +734,7 @@ void MainWindow::doubleClickTable(const QModelIndex& index)
|
||||
if(db.getObjectByName(ui->comboBrowseTable->currentText()).gettype() != "table")
|
||||
return;
|
||||
|
||||
editText(index.row(), index.column());
|
||||
editText(index);
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
@@ -125,7 +125,7 @@ private slots:
|
||||
virtual void helpAbout();
|
||||
virtual void updateRecordText(int row, int col, const QByteArray& newtext);
|
||||
virtual void editWinAway();
|
||||
virtual void editText( int row, int col );
|
||||
virtual void editText(const QModelIndex& index);
|
||||
virtual void doubleClickTable(const QModelIndex& index);
|
||||
virtual void executeQuery();
|
||||
virtual void importTableFromCSV();
|
||||
|
||||
@@ -205,6 +205,9 @@
|
||||
<property name="whatsThis">
|
||||
<string>This is the database view. You can double-click any record to edit its contents in the cell editor window.</string>
|
||||
</property>
|
||||
<property name="editTriggers">
|
||||
<set>QAbstractItemView::NoEditTriggers</set>
|
||||
</property>
|
||||
<property name="selectionMode">
|
||||
<enum>QAbstractItemView::ExtendedSelection</enum>
|
||||
</property>
|
||||
|
||||
@@ -475,6 +475,38 @@ bool DBBrowserDB::updateRecord(int wrow, int wcol, const QByteArray& wtext)
|
||||
}
|
||||
}
|
||||
|
||||
bool DBBrowserDB::updateRecord(const QString& table, const QString& column, int row, const QByteArray& value)
|
||||
{
|
||||
if (!isOpen()) return false;
|
||||
|
||||
lastErrorMessage = QString("no error");
|
||||
|
||||
QString sql = QString("UPDATE `%1` SET `%2`=? WHERE rowid=%3;").arg(table).arg(column).arg(row);
|
||||
|
||||
logSQL(sql, kLogMsg_App);
|
||||
setRestorePoint();
|
||||
|
||||
sqlite3_stmt* stmt;
|
||||
int success = 1;
|
||||
if(sqlite3_prepare_v2(_db, sql.toUtf8(), -1, &stmt, 0) != SQLITE_OK)
|
||||
success = 0;
|
||||
if(success == 1 && sqlite3_bind_text(stmt, 1, value.constData(), value.length(), SQLITE_STATIC) != SQLITE_OK)
|
||||
success = -1;
|
||||
if(success == 1 && sqlite3_step(stmt) != SQLITE_DONE)
|
||||
success = -1;
|
||||
if(success != 0 && sqlite3_finalize(stmt) != SQLITE_OK)
|
||||
success = -1;
|
||||
|
||||
if(success == 1)
|
||||
{
|
||||
return true;
|
||||
} else {
|
||||
lastErrorMessage = sqlite3_errmsg(_db);
|
||||
qCritical() << "updateRecord: " << lastErrorMessage;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool DBBrowserDB::browseTable( const QString & tablename, const QString& orderby )
|
||||
{
|
||||
QStringList testFields = getTableFields( tablename );
|
||||
|
||||
@@ -101,6 +101,7 @@ public:
|
||||
void updateSchema() ;
|
||||
bool addRecord(const QString& sTableName);
|
||||
bool deleteRecord(int wrow);
|
||||
bool updateRecord(const QString& table, const QString& column, int row, const QByteArray& value);
|
||||
bool updateRecord(int wrow, int wcol, const QByteArray& wtext);
|
||||
bool browseTable( const QString & tablename, const QString& orderby = "rowid" );
|
||||
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
#include "sqlitetablemodel.h"
|
||||
|
||||
#include "sqlitedb.h"
|
||||
|
||||
#include <QDebug>
|
||||
@@ -18,6 +17,12 @@ void SqliteTableModel::setChunkSize(size_t chunksize)
|
||||
m_chunkSize = chunksize;
|
||||
}
|
||||
|
||||
void SqliteTableModel::setTable(const QString& table)
|
||||
{
|
||||
m_sTable = table;
|
||||
setQuery(QString("SELECT * FROM `%1`").arg(table));
|
||||
}
|
||||
|
||||
void SqliteTableModel::setQuery(const QString& sQuery)
|
||||
{
|
||||
if(!m_db->isOpen())
|
||||
@@ -116,6 +121,24 @@ QVariant SqliteTableModel::data(const QModelIndex &index, int role) const
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
bool SqliteTableModel::setData(const QModelIndex& index, const QVariant& value, int role)
|
||||
{
|
||||
if(index.isValid() && role == Qt::EditRole)
|
||||
{
|
||||
m_data[index.row()].replace(index.column(), value.toString());
|
||||
|
||||
if(m_db->updateRecord(m_sTable, m_headers.at(index.column()), index.row()+1, value.toByteArray()))
|
||||
{
|
||||
emit(dataChanged(index, index));
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool SqliteTableModel::canFetchMore(const QModelIndex &parent) const
|
||||
{
|
||||
return m_data.size() < m_rowCount;
|
||||
@@ -150,3 +173,11 @@ void SqliteTableModel::fetchMore(const QModelIndex& parent)
|
||||
beginInsertRows(parent, currentsize, m_data.size());
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
Qt::ItemFlags SqliteTableModel::flags(const QModelIndex& index) const
|
||||
{
|
||||
if(!index.isValid())
|
||||
return Qt::ItemIsEnabled;
|
||||
|
||||
return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
|
||||
}
|
||||
|
||||
@@ -16,13 +16,17 @@ public:
|
||||
int columnCount(const QModelIndex &parent) const;
|
||||
QVariant headerData(int section, Qt::Orientation orientation, int role) const;
|
||||
QVariant data(const QModelIndex &index, int role) const;
|
||||
bool setData(const QModelIndex& index, const QVariant& value, int role = Qt::EditRole);
|
||||
bool canFetchMore(const QModelIndex &parent) const;
|
||||
void fetchMore(const QModelIndex &parent);
|
||||
size_t queryMore(size_t offset);
|
||||
|
||||
void setQuery(const QString& sQuery);
|
||||
void setTable(const QString& table);
|
||||
void setChunkSize(size_t chunksize);
|
||||
|
||||
Qt::ItemFlags flags(const QModelIndex& index) const;
|
||||
|
||||
signals:
|
||||
|
||||
public slots:
|
||||
@@ -35,6 +39,7 @@ private:
|
||||
QStringList m_headers;
|
||||
QMap<int, QStringList> m_data;
|
||||
QString m_sQuery;
|
||||
QString m_sTable;
|
||||
|
||||
size_t m_chunkSize;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user