我正在尝试从数组中获取数据库中不存在的值或数据。
public Cursor checkExistence(){
Cursor c=null;
String[] values={"headache","cold"};
SQLiteDatabase db= getReadableDatabase();
String query="SELECT * FROM "+TABLE_SYMPTOMS+" WHERE "+COLUMN_SYMP+" IN ("+toArrayRep(values)+")";
c=db.rawQuery(query,null);
Log.i("From Cursor","Cursor Count : " + c.getCount());
if(c.getCount()>0){
String val= c.getString()
Log.i("From Cursor","No insertion");
}else{
Log.i("From Cursor","Insertion");
}
db.close();
return c;
}
public static String toArrayRep(String[] in) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < in.length; i++) {
if (i != 0) {
result.append(",");
}
result.append("'" + in[i] + "'");
}
return result.toString();
}
在String values={"headache","cold"}
中,头痛存在,但数据库中不存在冷。从上面的代码中,Cursor返回Count=1
count>0
,因此我无法插入到表中。我想知道如何独立检查单个数据是否存在,以及是否存在将存在将插入到表中。因此,在这种情况下,“Cold”将能够插入到表中。
答案 0 :(得分:1)
如果您使用单个查询来检查所有值,那么您获得的是现有值列表,您仍然需要在原始列表中搜索任何差异。
单独检查每个值更简单:
<div class="container" >
<br>
<h1><B><font color="red" style= "font-size:70"> Ayoola's Calculator </font> </B></h1>
<form name="calculator" >
<input name = "display" placeholder="0" style= "width:254px; height:60px;
text-align:right; font-size:30; border-radius:8px; margin:3px"/>
<br>
<input name = "answer" placeholder="0" style= "width:254px; height:60px; text-align:right; font-size:30; border-radius:8px; margin:3px"/>
<br>
<input type="button" value= "7" name="seven" onClick="document.calculator.display.value+='7'" style="width:60px; font-size:30; margin:3px"/>
<input type="button" value= "8" name="eight" onClick="document.calculator.display.value+='8'" style="width:60px; font-size:30; "/>
<input type="button" value= "9" name="nine" onClick="document.calculator.display.value+='9'" style="width:60px; font-size:30; "/>
<input type="button" value= "*" name="multiply()" onClick="document.calculator.display.value+='*'" style="width:60px; font-size:30; "/>
<br>
<input type="button" value= "4" name="four" onClick="document.calculator.display.value+='4'" style="width:60px; font-size:30; margin:3px"/>
<input type="button" value= "5" name="five" onClick="document.calculator.display.value+='5'" style="width:60px; font-size:30; "/>
<input type="button" value= "6" name="six" onClick="document.calculator.display.value+='6'" style="width:60px; font-size:30; "/>
<input type="button" value= "-" name="minus()" onClick="document.calculator.display.value+='-'" style="width:60px; font-size:30; "/>
<br>
<input type="button" value= "1" name="one" onClick="document.calculator.display.value+='1'" style="width:60px; font-size:30; margin:3px"/>
<input type="button" value= "2" name="two" onClick="document.calculator.display.value+='2'" style="width:60px; font-size:30; "/>
<input type="button" value= "3" name="three" onClick="document.calculator.display.value+='3'" style="width:60px; font-size:30; "/>
<input type="button" value= "+" name="plus()" onClick="document.calculator.display.value+='+'" style="width:60px; font-size:30; "/>
<br>
<input type="button" value= "." name="point" onClick="document.calculator.display.value+='.'" style="width:60px; font-size:30; margin:3px"/>
<input type="button" value= "0" name="zero" onClick="document.calculator.display.value+='0'" style="width:60px; font-size:30; "/>
<input type="button" value= "/" name="divide()" onClick="document.calculator.display.value+='/'" style="width:60px; font-size:30; "/>
<input type="button" value= "=" name="equal" onClick="document.calculator.answer.value=eval(document.calculator.display.value)" style="width:60px; font-size:30; "/>
<br>
<input type="reset" value= "C" onClick= "clear()" style="width:256px; font-size:30; margin:3px "/>
</form>
答案 1 :(得分:0)
您需要检查光标。 moveToFirst()
我的代码示例:
return database.query( table.getNameTable(),
table.getColumns(),
table.getWhereSelectTableScript(),
null,
table.getGroupBySelectTableScript(),
table.getHavingSelectTableScript(),
table.getOrderBySelectTableScript(),
table.getLimitRecordsSelectTableScript());
查看更多here!