即使在Android sqlite中不为null,也返回null

时间:2015-01-15 06:30:21

标签: android android-sqlite

当sqlite

中的位置存在数据时,我得到一个空值
Cursor c = db.query("SELECT * FROM table WHERE id " = '" + value + "'", null);

if(c !=null && c.moveToFirst())
{
     temp = c.getString(c.getColumnIndex(b1));

     Log.v(TAG, ""+temp);
}

日志文件是

------------
Tag   Text
-------------
class null 

------------- 

该值存在,但我得到null可以整理出来。

如果cursor为null,显然应该跳过if语句,但它会执行语句。

对于其他一些值,我得到答案

Tag    Text
-----------
class  100
----------
class  86

感谢您给出的时间和回复

1 个答案:

答案 0 :(得分:0)

您没有撰写正确的SQL查询 这是正确的查询

Cursor c = db.rawQuery("SELECT * FROM "+ table +" WHERE id  = " + value , null);

你也可以使用像这样的db.query()方法

    Cursor cursor = db.query(TABLE_NAME, // a. table
            COLUMNS, // b. column names as array of strings

            KEY_ID + "= ?", // c. selections

            new String[] { String.valueOf(id) }, // d. selections args
            null, // e. group by
            null, // f. having
            null, // g. order by
            null); // h. limit

    // 3. if we got results get the first one
    if (cursor != null)
        cursor.moveToFirst();

    temp = cursor.getString(c.getColumnIndex(b1));