如何使用for循环android搜索sqlite数据库记录

Jac*_*ack 0 android android-sqlite

我想循环遍历sqlite数据库if if(johnny)是否存在于数据库中,但我的代码不起作用

if(!cursor.isAfterLast()) 
{
    do 
    {
        for(int i = 0; i<cursor.getCount(); i++)
        {
            if("johnny" == cursor.getString(1))
            { 
                Toast.makeText(getApplicationContext(), "string found!!! =)", Toast.LENGTH_LONG).show();
            }
        }                   
    } 
    while (cursor.moveToNext());
    }       
    cursor.close();
}
Run Code Online (Sandbox Code Playgroud)

Gir*_*hai 5

用于比较使用.equals()即改变

if("johnny" == cursor.getString(1))
Run Code Online (Sandbox Code Playgroud)

if("johnny".equals(cursor.getString(1)))
Run Code Online (Sandbox Code Playgroud)

编辑

您可以使用这样的查询

 Cursor objCursor = db.query("yourtableName",null, "columnName=?",
            new String[] { "johnny" }, null, null, null, null);

 if ((objCursor != null) && (objCursor.getCount() > 0)) {
                      Toast.makeText(getApplicationContext(), "string found!!! =)", Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud)

注意:替换yourtableName为您的表名和columnName表的列名.