std::string SQLite::convertConditionMap(const cocos2d::ValueMap &conditionMap) { std::string conditionStr; int count = conditionMap.size(); if (count > 0) { conditionStr.append(" WHERE "); int i = 0; for (auto iter = conditionMap.cbegin(); iter != conditionMap.cend(); ++iter) { std::string columnName = iter->first; std::string columnValue = iter->second.asString(); conditionStr.append(columnName); conditionStr.append("='"); conditionStr.append(columnValue); conditionStr.append("'"); if (i != count-1) { conditionStr.append(" AND "); } i++; } } return conditionStr; }
bool SQLite::insertIntoTable(const std::string &tableName, const cocos2d::ValueMap &keyValueMap) { std::string sql = "INSERT INTO "; sql.append(tableName); std::string keyStr = "("; std::string valueStr = "("; int count = keyValueMap.size(); int i = 0; for (auto iter = keyValueMap.cbegin(); iter != keyValueMap.cend(); ++iter) { std::string key = iter->first; std::string value = iter->second.asString(); keyStr.append(key); valueStr.append("'"); valueStr.append(value); valueStr.append("'"); if (i != count-1) { keyStr.append(","); valueStr.append(","); } i++; } keyStr.append(")"); valueStr.append(")"); sql.append(keyStr); sql.append(" VALUES "); sql.append(valueStr); return this->executeUpdate(sql); }
// 以下为更新接口 bool SQLite::updateTable(const std::string &tableName, const cocos2d::ValueMap &keyValueMap, const cocos2d::ValueMap &conditionMap) { std::string sql = "UPDATE "; sql.append(tableName); sql.append(" SET "); int count = keyValueMap.size(); int i = 0; for (auto iter = keyValueMap.cbegin(); iter != keyValueMap.cend(); ++iter) { std::string key = iter->first; std::string value = iter->second.asString(); sql.append(key); sql.append("='"); sql.append(value); sql.append("'"); if (i != count-1) { sql.append(","); } i++; } sql.append(this->convertConditionMap(conditionMap)); return this->executeUpdate(sql); }