我在编写sql语法时出错。我尝试通过使用“常规”字符替换特殊字符来简单地使用Room过滤实体中某些字段的名称。这是原始查询:
SimpleSQLiteQuery simple = new SimpleSQLiteQuery("SELECT * FROM station WHERE REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(LOWER(" + stationName + "), \"ž\", \"z\"), \"Ž\", \"z\"), \"č\", \"c\"), \"Č\", \"c\"), \"š\", \"s\"), \"Š\", \"s\"), \"ć\", \"c\"), \"Ć\", \"c\") LIKE " + "\" REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(LOWER(stationName), \"ž\", \"z\"), \"Ž\", \"z\"), \"č\", \"c\"), \"Č\", \"c\"), \"š\", \"s\"), \"Š\", \"s\"), \"ć\", \"c\"), \"Ć\", \"c\")"+ "\" ORDER BY stationName ASC");
我收到错误:E / SQLiteLog:(1)靠近“ž”:语法错误,我在editText中写什么字符都没有关系。我在这里错过了一些东西。感谢您的帮助!
答案 0 :(得分:0)
我找到了解决方法:
String sql = "SELECT * FROM station WHERE REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(LOWER(stationName), 'ž', 'z'), 'Ž', 'z'), 'č', 'c'), 'Č', 'c'), 'š', 's'), 'Š', 's'), 'ć', 'c'), 'Ć', 'c') LIKE REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(LOWER('" + station + "'), 'ž', 'z'), 'Ž', 'z'), 'č', 'c'), 'Č', 'c'), 'š', 's'), 'Š', 's'), 'ć', 'c'), 'Ć', 'c') ORDER BY stationName ASC";
Amit Jangid是正确的。我用单引号替换了所有!谢谢