Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite Query in Android to count rows

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.

like image 848
arun Avatar asked Mar 05 '11 06:03

arun


People also ask

How do I COUNT rows in SQLite?

SQLite Count(*) Function In SQLite the Count(*) function will return total number of rows available in a table, including the rows which contain NULL values. The Count(*) will not take any parameters other than the asterisk symbol (*).

How do I COUNT rows in SQL query?

To counts all of the rows in a table, whether they contain NULL values or not, use COUNT(*). That form of the COUNT() function basically returns the number of rows in a result set returned by a SELECT statement.

Does SQLite have a query optimizer?

The query optimizer in SQLite has basically two choices on how to implement this query. (There are actually six different choices, but we will only consider two of them here.) Pseudocode below demonstrating these two choices.

How many rows of data can SQLite handle?

The max_page_count PRAGMA can be used to raise or lower this limit at run-time. The theoretical maximum number of rows in a table is 264 (18446744073709551616 or about 1.8e+19). This limit is unreachable since the maximum database size of 281 terabytes will be reached first.


2 Answers

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}); 
like image 74
scottyab Avatar answered Sep 26 '22 02:09

scottyab


@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(); 

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

like image 22
ghchinoy Avatar answered Sep 27 '22 02:09

ghchinoy