Android 中的 SQLite 查询来计算行数
我正在尝试创建一个简单的登录表单,将在登录屏幕上输入的登录 ID 和密码与存储在数据库中的登录 ID 和密码进行比较。
我正在使用以下查询:
final String DATABASE_COMPARE =
"select count(*) from users where uname=" + loginname + "and pwd=" + loginpass + ");" ;
问题是,我不知道如何执行上述查询并存储返回的计数。
这是数据库表的样子(我已经使用 execSQl 方法成功创建了数据库)
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);";
有人可以指导我如何实现这一目标吗?如果可能,请提供示例片段来执行上述任务。
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.
I'm using the following query:
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.
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
DatabaseUtils.queryNumEntries(自 api:11 起)是有用的替代方案,无需原始 SQL(耶!)。
DatabaseUtils.queryNumEntries (since api:11) is useful alternative that negates the need for raw SQL(yay!).
@scottyab 参数化 DatabaseUtils.queryNumEntries(db, table, whereparams) 存在于 API 11 + 中,即 自 API 1 以来不存在 whereparams。答案必须是使用 db.rawQuery 创建一个 Cursor:
我也喜欢@Dre 的答案,使用参数化查询。
@scottyab the parametrized DatabaseUtils.queryNumEntries(db, table, whereparams) exists at API 11 +, the one without the whereparams exists since API 1. The answer would have to be creating a Cursor with a db.rawQuery:
I also like @Dre's answer, with the parameterized query.
使用 SQLiteStatement。
例如
Use an SQLiteStatement.
e.g.
请参阅 rawQuery(String, String[]) 和 的文档Cursor
您的 DADABASE_COMPARE SQL 语句当前无效,
loginname
和loginpass
不会被转义,loginname
和loginname
之间没有空格和
,并且以 ) 结束语句;而不是 ; -- 如果您以 bob 身份使用密码登录,则该语句最终将显示为Also, you should might use the SelectionArgs feature,而不是连接登录名和登录密码。
要使用selectionArgs,你会做类似的事情
See rawQuery(String, String[]) and the documentation for Cursor
Your DADABASE_COMPARE SQL statement is currently invalid,
loginname
andloginpass
won't be escaped, there is no space betweenloginname
and theand
, and you end the statement with ); instead of ; -- If you were logging in as bob with the password of password, that statement would end up asAlso, you should probably use the selectionArgs feature, instead of concatenating loginname and loginpass.
To use selectionArgs you would do something like
假设您已经建立了数据库(
db
)连接,我认为最优雅的方法是坚持使用Cursor
类,并执行以下操作:Assuming you already have a Database (
db
) connection established, I think the most elegant way is to stick to theCursor
class, and do something like:如何获取计数列
这是最简洁和精确的替代方案。无需处理游标及其关闭。
how to get count column
This is the most concise and precise alternative. No need to handle cursors and their closing.
如果您使用 ContentProvider 那么您可以使用:
If you are using ContentProvider then you can use:
如果您想获取记录数,则必须在某些字段上应用分组依据或应用以下查询。
喜欢
If you want to get the count of records then you have to apply the group by on some field or apply the below query.
Like
另一种方法是
在像这样的游标上使用:
如果您能想到摆脱原始查询。
Another way would be using:
on a Cursor like:
If you can think of getting away from the raw query.