我正在从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,但是使用上面的查询永远不会发生”
答案 0 :(得分:0)
我认为您的问题是对逻辑的误解。假设数据库中的数据例如(按照下面的示例):-
Difficulty = 1, IsRejected = 0, IsSlected 0, Topic = "People"
Difficulty = 1, IsRejected = 0, IsSlected 0, Topic = "Leisure"
Difficulty = 1, IsRejected = 0, IsSlected 0, Topic = "Not a Listed Topic"
Difficulty = 1, IsRejected = 1, IsSlected 0, Topic = "People"
Difficulty = 1, IsRejected = 0, IsSlected 1, Topic = "People"
Difficulty = 1, IsRejected = 1, IsSlected 1, Topic = "People"
Difficulty = 2, IsRejected = 0, IsSlected 0, Topic = "People"
所有选定主题的列表为:-
然后只有1和2被 SELECT 选择为:-
3-7将不会提取为:-
AND (Topic = ? OR ......)
为假(不是真的)AND isRejected
为假,因此括号中的3 AND为假,因此总结果为假。基本上,在使用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