Skip to content
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

Replace single quotes w/ correct escape #386

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions source/sqlHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ bool insertRowIntoDB(sqlite3* db, const String tableName, const Array<String>& v
// Note that ID does not need to be provided unless PRIMARY KEY is set.
String insertC = "INSERT INTO " + tableName + colNames + " VALUES(";
for (int i = 0; i < values.size(); i++) {
insertC += values[i];
String insertVal = values[i];
if (beginsWith(values[i], "'")) { // Check if we are handling a single-quote delimited string
insertVal = values[i].substr(1, values[i].length() - 2);
replaceSingleQuote(insertVal);
insertVal = "'" + insertVal + "'";
}
insertC += insertVal;
if(i < values.size() - 1) insertC += ",";
}
insertC += ");";
//logPrintf("Inserting row into %s table w/ SQL query:%s\n\n", tableName.c_str(), insertC.c_str());

char* errmsg;
int ret = sqlite3_exec(db, insertC.c_str(), 0, 0, &errmsg);
if (ret != SQLITE_OK) {
Expand All @@ -60,7 +66,13 @@ bool insertRowsIntoDB(sqlite3* db, const String tableName, const Array<Array<Str
for (int i = 0; i < value_vector.size(); ++i) {
insertC += "(";
for (int j = 0; j < value_vector[i].size(); j++) {
insertC += value_vector[i][j];
String insertVal = value_vector[i][j];
if (beginsWith(value_vector[i][j], "'")) { // Check if we are handling a single-quote delimited string
insertVal = value_vector[i][j].substr(1, value_vector[i][j].length() - 2);
replaceSingleQuote(insertVal);
insertVal = "'" + insertVal + "'";
}
insertC += insertVal;
if (j < value_vector[i].size() - 1) insertC += ",";
}
insertC += ")";
Expand All @@ -80,3 +92,14 @@ bool insertRowsIntoDB(sqlite3* db, const String tableName, const Array<Array<Str
return ret == SQLITE_OK;
}

bool replaceSingleQuote(String& str) {
size_t pos;
size_t lastPos = 0;
bool found = false;
while ((pos = str.find('\'', lastPos)) != String::npos) {
found = true;
str = str.substr(0, pos) + "'" + str.substr(pos);
lastPos = pos + 2;
}
return found;
}
1 change: 1 addition & 0 deletions source/sqlHelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@
bool createTableInDB(sqlite3* db, const String tableName, const Array<Array<String>>& columns);
bool insertRowIntoDB(sqlite3* db, const String tableName, const Array<String>& values, const String colNames = "");
bool insertRowsIntoDB(sqlite3* db, const String tableName, const Array<Array<String>>& valueVector, const String colNames = "");
bool replaceSingleQuote(String& str);