From bed67cd9709caad17146ab4e0c90952988a21317 Mon Sep 17 00:00:00 2001 From: Martin Kleusberg Date: Sat, 5 Jan 2013 15:31:42 +0100 Subject: [PATCH] Make sure to always escape table and column names properly Always put `...` around the names of tables, columns and other objects to avoid errors when the user creates objects with the name of key words. --- src/CreateIndexForm.cpp | 6 ++---- src/EditTableForm.cpp | 2 +- src/ImportCSVForm.cpp | 13 ++++--------- src/MainWindow.cpp | 2 +- src/sqlbrowser_util.c | 2 +- src/sqlitedb.cpp | 29 +++++------------------------ 6 files changed, 14 insertions(+), 40 deletions(-) diff --git a/src/CreateIndexForm.cpp b/src/CreateIndexForm.cpp index ef98f4ad..0c55bcba 100644 --- a/src/CreateIndexForm.cpp +++ b/src/CreateIndexForm.cpp @@ -69,9 +69,9 @@ void createIndexForm::confirmCreate() if (comboUnique->currentIndex()==1){ createStatement.append("UNIQUE "); } - createStatement.append("INDEX "); + createStatement.append("INDEX `"); createStatement.append(indexLineEdit->text()); - createStatement.append(" ON "); + createStatement.append("` ON "); createStatement.append(comboTables->currentText()); createStatement.append("("); createStatement.append(comboFields->currentText()); @@ -102,5 +102,3 @@ void createIndexForm::populateTable(const QList& rmap) }*/ } } - - diff --git a/src/EditTableForm.cpp b/src/EditTableForm.cpp index 89a2b9ce..c82b6bb9 100644 --- a/src/EditTableForm.cpp +++ b/src/EditTableForm.cpp @@ -71,7 +71,7 @@ void editTableForm::accept() // Build SQL statement from what the use entered QString sql = QString("CREATE TABLE `%1` (").arg(ui->editTableName->text()); for(int i=0;itreeWidget->topLevelItemCount();i++) - sql.append(QString("%1 %2,").arg(ui->treeWidget->topLevelItem(i)->text(0)).arg(ui->treeWidget->topLevelItem(i)->text(1))); + sql.append(QString("`%1` %2,").arg(ui->treeWidget->topLevelItem(i)->text(0)).arg(ui->treeWidget->topLevelItem(i)->text(1))); sql.remove(sql.count() - 1, 1); // Remove last comma sql.append(");"); diff --git a/src/ImportCSVForm.cpp b/src/ImportCSVForm.cpp index 0087a99d..e9adb585 100644 --- a/src/ImportCSVForm.cpp +++ b/src/ImportCSVForm.cpp @@ -110,11 +110,9 @@ void importCSVForm::createButtonPressed() QProgressDialog progress("Inserting data...", "Cancel", 0, curList.size()); progress.setWindowModality(Qt::ApplicationModal); - sql = "CREATE TABLE "; - sql.append(tabname); - sql.append(" ("); + sql = QString("CREATE TABLE `%1` (").arg(tabname); for (int r=0; rdb,stmt, callback, &d2, 0); sqlite3_free(stmt); set_table_name(&d2, 0); diff --git a/src/sqlitedb.cpp b/src/sqlitedb.cpp index 3317722d..f480fbeb 100644 --- a/src/sqlitedb.cpp +++ b/src/sqlitedb.cpp @@ -309,9 +309,7 @@ bool DBBrowserDB::addRecord ( ) int fields = browseFields.count(); QString emptyvalue = curNewData; - QString statement = "INSERT INTO "; - statement.append(GetEncodedQString(curBrowseTableName)); - statement.append(" VALUES("); + QString statement = QString("INSERT INTO `%1` VALUES(").arg(GetEncodedQString(curBrowseTableName)); for ( int i=1; i<=fields; i++ ) { statement.append(emptyvalue); if (i