如何在查询SQLite查询语句中使用拖曳运算符?

时间:2019-06-15 10:49:56

标签: android-sqlite

我正在从SQLite数据库中查询,我需要使用以下代码在单个查询中组合两个运算符“ AND”和“ OR”。光标不返回数据。如何在查询语句中使用拖曳运算符?

PREFIX : <http://www.richarddegenne.com/ontology/astronomy#>

# Get all statements about :star1086
SELECT * WHERE {
  :star1086 ?predicate ?object
}

# Get some statement about :star1086
SELECT * WHERE {
  VALUES ?predicate {
    :hasSpectralType :temperate
  }
  :star1086 ?predicate ?object
}

# Ask whether a given pattern is true
ASK WHERE {
  :star1086 :hasSpectralType :yellowDwarf
}

# Filter stars based on their temperature
# Note: You might want to add more stars with different temperature
#       if you want useful results.
SELECT ?star WHERE {
  ?star :temperature ?temperature
  FILTER(?temperature > 5000)
}

日志显示:-

    String[] selectedTopics = getSelectedTopics();
    String[] selectedArgs = new String[selectedTopics.length + 3];

    String selection = "(" + COLUMN_FOREIGN_WORDS_DIFFICULTY + " = ? AND " 
        +   COLUMN_FOREIGN_WORDS_IS_SELECTED + " = ? AND "  
        +   COLUMN_FOREIGN_WORDS_IS_REJECTED + " = ? ) AND (";
    for (int i = 0; i < (selectedTopics.length); i++) {
        if (i != (selectedTopics.length - 1)) {
            selection += "topic = ? OR ";
        } else {
            selection += "topic = ? ) ";
        }
    }

    selectedArgs[0] = String.valueOf(difficulty);
    selectedArgs[1] = "0";
    selectedArgs[2] = "0";
    int k = 0;
    for (int j = 3; j < selectedArgs.length; j++) {
        selectedArgs[j] = selectedTopics[k];
        k++;
    }

    Log.i(TAG, "getNewWordsToLearn: selection = " + selection);
    Log.i(TAG, "getNewWordsToLearn: selectionsArgs = " + 
           Arrays.toString(selectedArgs));

   Cursor c = database.query(TABLE_NAME_FOREIGN_WORDS,null, selection, 
      selectedArgs, null, null, null);

“我希望c.getCount> 0,但是使用上面的查询永远不会发生”

1 个答案:

答案 0 :(得分:0)

我认为您的问题是对逻辑的误解。假设数据库中的数据例如(按照下面的示例):-

  1. Difficulty = 1, IsRejected = 0, IsSlected 0, Topic = "People"
  2. Difficulty = 1, IsRejected = 0, IsSlected 0, Topic = "Leisure"
  3. Difficulty = 1, IsRejected = 0, IsSlected 0, Topic = "Not a Listed Topic"
  4. Difficulty = 1, IsRejected = 1, IsSlected 0, Topic = "People"
  5. Difficulty = 1, IsRejected = 0, IsSlected 1, Topic = "People"
  6. Difficulty = 1, IsRejected = 1, IsSlected 1, Topic = "People"
  7. Difficulty = 2, IsRejected = 0, IsSlected 0, Topic = "People"

所有选定主题的列表为:-

  • 外观,外出就餐,环境,食品,健康,家庭,休闲,人,政治,参考,服务,体育,学习,运输,工作

然后只有1和2被 SELECT 选择为:-

  • 难度为1
  • AND IsRejected为0
  • AND IsSelected为0
  • 并且主题 / 休闲在所选主题列表中

3-7将不会提取为:-

    1. 不是列出的主题不是列表中的主题,因此AND (Topic = ? OR ......)为假(不是真的)
    1. IsRejected 不是0,所以AND isRejected为假,因此括号中的3 AND为假,因此总结果为假。
    1. 已选择不是0(结果与4相同)
    1. 已被拒绝已被选中均为假(因此结果与4相同)
    1. 难度不是1(结果与4相同)。

基本上,在使用AND时,表达式的两边都必须为TRUE才能使表达式为TRUE。

上面是根据以下代码对代码进行稍微修改的测试:-

public void doit(int difficulty, String[] topicsSelected) {

    String TAG = "DBQueries";
    String[] selectedTopics = topicsSelected; //<<<<<<<<<< get the topics from parameter passed
    String[] selectedArgs = new String[selectedTopics.length + 3];

    String selection = "(" + COLUMN_FOREIGN_WORDS_DIFFICULTY + " = ? AND "
            + COLUMN_FOREIGN_WORDS_IS_SELECTED + " = ? AND "
            + COLUMN_FOREIGN_WORDS_IS_REJECTED + " = ? ) AND (";
    for (
            int i = 0; i < (selectedTopics.length); i++) {
        if (i != (selectedTopics.length - 1)) {
            selection += "topic = ? OR ";
        } else {
            selection += "topic = ? ) ";
        }
    }

    selectedArgs[0] = String.valueOf(difficulty);
    selectedArgs[1] = "0";
    selectedArgs[2] = "0";
    int k = 0;
    for (
            int j = 3;
            j < selectedArgs.length; j++) {
        selectedArgs[j] = selectedTopics[k];
        k++;
    }

    Log.i(TAG, "getNewWordsToLearn: selection = " + selection);
    Log.i(TAG, "getNewWordsToLearn: selectionsArgs = " +
            Arrays.toString(selectedArgs));

    Cursor c = database.query(TABLE_NAME_FOREIGN_WORDS, null, selection,
            selectedArgs, null, null, null);
    //<<<<<<<<<< ADDED CODE
    Log.i(TAG,"Number of rows extracted is " + String.valueOf(c.getCount()));
    while (c.moveToNext()) {
        Log.i(TAG,"Extracted row is:-  Topic is " + c.getString(c.getColumnIndex(COLUMN_FOREIGN_WORDS_TOPIC))   );
    }
}
  • 查看评论以了解变化

同时使用以下方法(也在数据库助手类中)允许添加一些测试数据:-

public long add(int difficulty, boolean rejected, boolean selected, String topic) {
    ContentValues cv = new ContentValues();
    cv.put(COLUMN_FOREIGN_WORDS_DIFFICULTY,difficulty);
    cv.put(COLUMN_FOREIGN_WORDS_IS_REJECTED,rejected);
    cv.put(COLUMN_FOREIGN_WORDS_IS_SELECTED,selected);
    cv.put(COLUMN_FOREIGN_WORDS_TOPIC,topic);
    return database.insert(TABLE_NAME_FOREIGN_WORDS,null,cv);
}

然后在“活动”中使用以下内容添加一些数据,然后调用您的代码( doit 方法):-

public class MainActivity extends AppCompatActivity {

    DBHelper002 mDBHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mDBHelper = new DBHelper002(this);
        String[] selectedtopics = new String[]{"Appearance", "Eating out", "Environment", "Food", "Health, Home", "Leisure", "People", "Politics", "Reference", "Services", "Sports", "Study", "Transport", "Work"};
        mDBHelper.add(1,false,false,"People");
        mDBHelper.add(1,false,false,"Leisure");
        mDBHelper.add(1,false,false,"Not a Listed Topic"); // Not Extracted as Not a Listed Topic is not one of the selected topics
        mDBHelper.add(1,true,false,"People"); //Not extracted as rejected is true (not 0) otherwise it would be selected
        mDBHelper.add(1,false,true,"People"); //Not extracted as selected is true (not 0) otherwise it would be selected
        mDBHelper.add(1,true,true,"People"); //Not extracted as rejected is true (not 0) and because selected is true
        mDBHelper.add(2,false,false,"People"); //Not Extracted as difficulty is not 1 otherwise is would be selected
        mDBHelper.doit(1,selectedtopics);
    }
}

结果是:-

06-16 07:07:17.932 32648-32648/? I/DBQueries: getNewWordsToLearn: selection = (diff = ? AND selected = ? AND rejected = ? ) AND (topic = ? OR topic = ? OR topic = ? OR topic = ? OR topic = ? OR topic = ? OR topic = ? OR topic = ? OR topic = ? OR topic = ? OR topic = ? OR topic = ? OR topic = ? OR topic = ? ) 
06-16 07:07:17.932 32648-32648/? I/DBQueries: getNewWordsToLearn: selectionsArgs = [1, 0, 0, Appearance, Eating out, Environment, Food, Health, Home, Leisure, People, Politics, Reference, Services, Sports, Study, Transport, Work]
06-16 07:07:17.932 32648-32648/? I/DBQueries: Number of rows extracted is 2
06-16 07:07:17.932 32648-32648/? I/DBQueries: Extracted row is:-  Topic is People
06-16 07:07:17.932 32648-32648/? I/DBQueries: Extracted row is:-  Topic is Leisure