问题描述
我正在尝试创建一个简单的登录表单,将在登录屏幕上输入的登录 ID 和密码与存储在数据库中的登录 ID 和密码进行比较.
I'm trying to create a simple Login form, where I compare the login id and password entered at the login screen with that stored in the database.
我正在使用以下查询:
final String DATABASE_COMPARE =
"select count(*) from users where uname=" + loginname + "and pwd=" + loginpass + ");" ;
问题是,我不知道如何执行上述查询并存储返回的计数.
The issue is, I don't know, how can I execute the above query and store the count returned.
这是数据库表的样子(我已经成功地使用 execSQl 方法创建了数据库)
Here's how the database table looks like ( I've manged to create the database successfully using the execSQl method)
private static final String
DATABASE_CREATE =
"create table users (_id integer autoincrement, "
+ "name text not null, uname primary key text not null, "
+ "pwd text not null);";//+"phoneno text not null);";
有人可以指导我如何实现这一目标吗?如果可能,请提供一个示例片段来完成上述任务.
Can someone kindly guide me as to how I can achieve this? If possible please provide a sample snippet to do the above task.
推荐答案
DatabaseUtils.queryNumEntries(从 api:11 开始)是有用的替代方案,它不需要原始 SQL(耶!).
DatabaseUtils.queryNumEntries (since api:11) is useful alternative that negates the need for raw SQL(yay!).
SQLiteDatabase db = getReadableDatabase();
DatabaseUtils.queryNumEntries(db, "users",
"uname=? AND pwd=?", new String[] {loginname,loginpass});
这篇关于Android 中的 SQLite 查询来计算行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!