Android的SQLite的查询在选择问号,并在selectionArgs两个空 [英] Android SQLite query has question mark in selector and null in selectionArgs
问题描述
在安卓如果我叫像下面的SQLiteDatabase方式查询:
In android if I call the SQLiteDatabase method query like the following:
sqlitedb.query("mytable", null, "name = ?",
new String[] {null}, null, null, null);
什么是预期的结果?
What is the expected result?
我所期望的 NullPointerException异常
但似乎我越来越似乎有相同的数据,因为我执行的最后一个非空selectionArgs两个查询上的游标回来。这是预期的行为,我想我必须做我自己的空检查调用此方法前,而是通过名称为空,以得到正确的结果?
I would expect a NullPointerException
but it seems like I am getting a cursor back that seems to have the same data as the last non-null selectionArgs query I performed. Is this expected behavior, I guess I have to do my own null checking before calling this method and instead pass "name IS NULL" to get the correct result?
推荐答案
这无关与NPE @ satur9nine。它与SQL参数procressing做。我已经验证你是正确的。使用SQLite,如果您发出:
This has nothing to do with NPE @satur9nine. It has to do with SQL argument procressing. I've verified that you are correct. With SQLite, if you issue:
SELECT * FROM `mytable ` WHERE `name` = ?
跨越其与空名称
字段行的表空的说法,你会得到任何结果。你将不得不发出查询:
with a null argument across a table which has rows with null name
fields, you will get no results. You are going to have to issue the query:
SELECT * FROM `mytable ` WHERE `name` IS NULL
这不仅是Android的,而且JDBC。这是一个数据库的问题。 SQLite的,H2和MySQL中的所有行为这种方式。从这个伟大的SQL教程:
This is not just Android but also JDBC. It is a database issue. SQLite, H2, and MySQL all behave this way. From this great SQL tutorial:
http://www.w3schools.com/sql/sql_null_values.asp
要报价:
这是不可能的,以测试NULL值与比较运算符,如=,&所述;或&所述;>
It is not possible to test for NULL values with comparison operators, such as =, <, or <>.
这篇关于Android的SQLite的查询在选择问号,并在selectionArgs两个空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!