sqlitedatabase android 中的游标计数总是 returns 0
cursor count always returns 0 in sqlitedatabase android
我有一个简单的方法来检查 sqlite 数据库中的现有用户名。如果它存在 return true else false.
但是 cursor.getcount() 总是 return 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;
}
您将常量 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);
我有一个简单的方法来检查 sqlite 数据库中的现有用户名。如果它存在 return true else false.
但是 cursor.getcount() 总是 return 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;
}
您将常量 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);