SQLite的查询在Android的计数行 [英] SQLite Query in Android to count rows

查看:394
本文介绍了SQLite的查询在Android的计数行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个简单的登录表单,在那里我比较进入在登录屏幕与存储在数据库中的登录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.

下面是如何在数据库表的样子(我manged使用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.

推荐答案

@scottyab的参数化<一href="http://developer.android.com/reference/android/database/DatabaseUtils.html#queryNumEntries%28android.database.sqlite.SQLiteDatabase,%20java.lang.String,%20java.lang.String%29">DatabaseUtils.queryNumEntries(db,表,whereparams)存在于API 11 +,一个<一个href="http://developer.android.com/reference/android/database/DatabaseUtils.html#queryNumEntries%28android.database.sqlite.SQLiteDatabase,%20java.lang.String%29">without在whereparams的存在,因为API 1 。答案就必须创建一个db.rawQuery一个光标:

@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:

Cursor mCount= db.rawQuery("select count(*) from users where uname='" + loginname + "' and pwd='" + loginpass +"'", null);
mCount.moveToFirst();
int count= mCount.getInt(0);
mCount.close();

我也很喜欢@ Dre的答案,用参数化查询。

I also like @Dre's answer, with the parameterized query.

这篇关于SQLite的查询在Android的计数行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆