我之前尝试过这样的陈述:
public Cursor searchForSchools(String[] args) throws SQLException {
String myQuery = "SELECT skola._id, skola.naziv, skola.privatna, skola.adresa, opstina.naziv, skola.broj_ucenika, skola.telefon, skola.web, oblast.naziv " +
"FROM skola JOIN opstina ON skola.id_opstina = opstina._id " +
"JOIN profil ON skola.id_tip = profil._id " +
"JOIN oblast_m ON profil._id = oblast_m.id_profil " +
"JOIN oblast ON oblast_m.id_oblast = oblast._id ";
return database.rawQuery(myQuery + "WHERE skola.naziv LIKE \"%?%\" AND opstina.naziv = ? AND skola.broj_ucenika ? AND skola.oblasti LIKE \"%?%\" AND skola.privatna ?", args);
但是由于SQL语法错误,这没有用。
现在,我正在尝试使用以下方法构建子句的第二部分:
public Cursor searchForSchools(String[] args) throws SQLException {
String myQuery = "SELECT skola._id, skola.naziv, skola.privatna, skola.adresa, opstina.naziv, skola.broj_ucenika, skola.telefon, skola.web, oblast.naziv " +
"FROM skola JOIN opstina ON skola.id_opstina = opstina._id " +
"JOIN profil ON skola.id_tip = profil._id " +
"JOIN oblast_m ON profil._id = oblast_m.id_profil " +
"JOIN oblast ON oblast_m.id_oblast = oblast._id ";
return database.rawQuery(myQuery + buildQueryString(args), null);
}
private String buildQueryString(String[] args) {
String buildString="WHERE";
if(args[0]!="*")
buildString += " skola.naziv LIKE %\"" + args[0] + "\" AND";
if(args[1]!="*")
buildString += " opstina.naziv = \"" + args[1] + "\" AND";
if(args[2]!="*")
buildString += " skola.broj_ucenika " + args[2] + "\" AND";
if(args[3]!="*")
buildString += " oblast.naziv LIKE %\"" + args[3] + "\" AND";
buildString += " skola.privatna " + args[4];
return buildString;
}
问题在于,有时args数组包含''(星号),表示查询与该列无关(即任何值都可以接受),但是在第一种调用方法中,像“WHERE opstina.naziv =''”这样的条款会返回错误......
让我解释一下我的目标 - 我想创建一个动态SQL语句,它可以处理返回所有数据的所有情况(当args [] = {'',''时, '','',''}}到最复杂的情况,每个args字符串的值都与“”不同。提前谢谢......