vendredi 9 juillet 2021

SQL syntax error encountered in MySQL query

I am getting the following error while executing the below query

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= '5746015658' THEN 10 END AS OrderColumn FROM `rio.lookup` WHERE rio_LookUp_Var' at line 1

I am generating the query in my C++ code snippet which is as follows

    std::string mysqlQuery;
    if(conf_mysql_.isDoNotPairNode())
    {
        mysqlQuery = "SELECT " + conf_mysql_.getDoNotPairColumn() + ", ";
    }
    else
    {
        mysqlQuery = "SELECT " + generateColumnNameString() + ", ";
    }
    
    std::string case_clause = "CASE";
    std::string where_clause = "WHERE ";

    std::vector<lookupQuerySpecifications> mysql_query_specifications = conf_mysql_.getMySQL_QuerySpecs(); 
    int case_clause_count = 0;
    for (auto it = begin (mysql_query_specifications); it != end (mysql_query_specifications); ++it) 
    {
        std::string lookupVal = getLookupValBasedOnType(it->lookup_value, it->lookup_type);
        if(lookupVal.empty())
        {
            continue;
        }
        else
        {
            if(it != begin(mysql_query_specifications) && status == true)
            {
                where_clause += lookupConsts::OR_OPERATOR + " ";
            }
            where_clause += it->lookup_key + " = '" + lookupVal + "' ";              

            status = true;
            case_clause += " WHEN " + it->lookup_key + " = '" + lookupVal + "' THEN " + std::to_string(case_clause_count);
            case_clause_count++;
        }
    }
    case_clause += " END AS OrderColumn";
    where_clause += " ORDER BY OrderColumn";
    mysqlQuery += case_clause + " FROM `" + conf_mysql_.getMySQL_collection_name() + "` " + where_clause;
    query = mysqlQuery;

Can anyone point out the issue here. Thanks

Aucun commentaire:

Enregistrer un commentaire