I wrote the following lines of code to compare some strings with contents of columns (string) in SQLite database:
void PaymentTransaction::setAttributesBySqliteStmt(sqlite3_stmt * statement)
{
ZF_LOGD("setAttributesBySqliteStmt\n");
int columnCnt = sqlite3_column_count(statement);
for (int i = 0; i < columnCnt; i++) {
if (strcmp("id", sqlite3_column_name(statement, i)) == 0)
id = sqlite3_column_int(statement, i);
else if (strcmp("gas_station_id", sqlite3_column_name(statement, i)) == 0)
gas_station_id = sqlite3_column_int(statement, i);
else if (strcmp("nozzle_id", sqlite3_column_name(statement, i)) == 0)
nozzle_id = sqlite3_column_int(statement, i);
................
//SAME CODES FOR OTHER COLUMNS
................
else {
ZF_LOGW("Invalid column name(%s) in table name Transaction",
sqlite3_column_name(statement, i));
}
}
}
void PaymentTransaction::bindValueToSqliteStmt(sqlite3_stmt* statement)
{
int paramCnt = sqlite3_bind_parameter_count(statement);
for (int i = 1; i <= paramCnt; i++) {
if (i == sqlite3_bind_parameter_index(statement, "@id"))
sqlite3_bind_int(statement, i, id);
else if (i == sqlite3_bind_parameter_index(statement, "@gas_station_id"))
sqlite3_bind_int(statement, i, gas_station_id);
else if (i == sqlite3_bind_parameter_index(statement, "@nozzle_id"))
sqlite3_bind_int(statement, i, nozzle_id);
else if ............
//SAME CODES FOR OTHER COLUMNS
..................
else {
ZF_LOGW("Invalid param name(%s) in table name Transaction",
sqlite3_bind_parameter_name(statement, i));
}
}
}
Since there are 30 columns in total, there are lots of if else statements
and I was wondering whether there is better/shorter way of writing these lines of code?