游标计数总是在sqlitedatabase android中返回0

时间:2016-03-17 14:01:25

标签: android sqlite cursor

我有一个简单的方法来检查sqlitedatabase中的现有用户名。如果它存在则返回true,否则为false。

但是无论我如何更改查询,cursor.getcount()始终返回0。

这是我的代码:

public boolean IsUsernameexists(String username)
    {
        boolean b=false;
        int count=0;


        Cursor cursor=mDatabase.query(TABLE_NAME,
                new String[]{USER_NAME,PASSWORD,SECURITY_Q_1,SECURITY_Q_2,SECURITY_Q_3}, " user_name=?", new String[]{USER_NAME}, null, null, null);




        Logger.log(Level.INFO, "Get count of username exists=", "" +  cursor.getCount());// always return 0
        if(count>=1)
        {
            b=true;
            Logger.log(Level.INFO,TAG,"Username already exists inside table");
            return b;


        }
        Logger.log(Level.INFO,TAG,"Username is new  inside table");
        return b;

    }

1 个答案:

答案 0 :(得分:3)

将常量USER_NAME传递给查询而不是方法参数username。也许您在USER_NAME常量中还有另一个值作为要提供的值user_name作为要查询的列。

尝试更改该行:

Cursor cursor=mDatabase.query(TABLE_NAME,
            new String[]{USER_NAME,PASSWORD,SECURITY_Q_1,SECURITY_Q_2,SECURITY_Q_3}, " user_name=?", new String[]{USER_NAME}, null, null, null);

为:

Cursor cursor=mDatabase.query(TABLE_NAME,
            new String[]{USER_NAME,PASSWORD,SECURITY_Q_1,SECURITY_Q_2,SECURITY_Q_3}, USER_NAME+"=?", new String[]{username}, null, null, null);