Search code examples
pythonmysqlsql-like

Space in like in MySQL search AWS python


Trying to query AWS database (newbie to this). Query is as below:

dfHealthData = pd.read_sql("SELECT YearStart, LocationDesc, Topic, Question, DataValue "
                               "FROM HealthData WHERE YearStart = '2018' AND DataValueType = 'Crude Prevalence' AND Question LIKE '%18 years'" , connection_string)   

Want to get string which ends in ' 18 years' (or ideally '>=18 years', but the former will do).

Error is as below. Error retrieving from AWS: unsupported format character ' ' (0x20) at index 161


Solution

  • % has a special meaning in python. Try to escape it:

    dfHealthData = pd.read_sql("SELECT YearStart, LocationDesc, Topic, Question, DataValue FROM HealthData WHERE YearStart = '2018' AND DataValueType = 'Crude Prevalence' AND Question LIKE '%%18 years'" , connection_string)