SQLite Query in Android for row counting

I am trying to create a simple login form where I compare the login ID and password entered on the login screen with what is stored in the database.

I am using the following query:

final String DATABASE_COMPARE = "select count(*) from users where uname=" + loginname + "and pwd=" + loginpass + ");" ; 

The problem is that I do not know how I can fulfill the above request and save the returned invoice.

This is what the database table looks like (I managed 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 explain to me how I can achieve this? If possible, provide a sample snippet to complete the above task.

+90
android sqlite count
Mar 05 2018-11-11T00:
source share
11 answers

DatabaseUtils.queryNumEntries (since api: 11) is a useful alternative that negates raw SQL (yay!).

 SQLiteDatabase db = getReadableDatabase(); DatabaseUtils.queryNumEntries(db, "users", "uname=? AND pwd=?", new String[] {loginname,loginpass}); 
+119
Jan 15 '12 at 17:10
source share

@scottyab parameterized DatabaseUtils.queryNumEntries (db, table, whereparams) exists in API 11 +, one without whereparams exists with API 1 . The answer would be to create a cursor with 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 a parameterized query.

+111
Jan 18 2018-12-18T00:
source share

Use SQLiteStatement .

eg.

  SQLiteStatement s = mDb.compileStatement( "select count(*) from users where uname='" + loginname + "' and pwd='" + loginpass + "'; " ); long count = s.simpleQueryForLong(); 
+62
Mar 21 '11 at 17:42
source share

See rawQuery (String, String []) and the documentation for Cursor

Your DADABASE_COMPARE SQL statement is currently invalid, loginname and loginpass will not be escaped, there is no space between loginname and and , and you end the statement); instead; - If you logged in as bob with a password, this statement would end as

 select count(*) from users where uname=boband pwd=password); 

In addition, you should probably use the selectionArgs function, and not combine the login and login.

To use selectionArgs, you would do something like

 final String SQL_STATEMENT = "SELECT COUNT(*) FROM users WHERE uname=? AND pwd=?"; private void someMethod() { Cursor c = db.rawQuery(SQL_STATEMENT, new String[] { loginname, loginpass }); ... } 
+22
Mar 05 '11 at 7:50
source share

Assuming you already have a database connection ( db ), I think the most elegant way is to stick with the Cursor class and do something like:

 String selection = "uname = ? AND pwd = ?"; String[] selectionArgs = {loginname, loginpass}; String tableName = "YourTable"; Cursor c = db.query(tableName, null, selection, selectionArgs, null, null, null); int result = c.getCount(); c.close(); return result; 
+22
Jan 14 '13 at 10:19
source share

how to get the column counter

 final String DATABASE_COMPARE = "select count(*) from users where uname="+loginname+ "and pwd="+loginpass; int sometotal = (int) DatabaseUtils.longForQuery(db, DATABASE_COMPARE, null); 

This is the shortest and most accurate alternative. No need to handle cursors and their closure.

+12
May 6 '12 at
source share

If you are using ContentProvider, you can use:

 Cursor cursor = getContentResolver().query(CONTENT_URI, new String[] {"count(*)"}, uname=" + loginname + " and pwd=" + loginpass, null, null); cursor.moveToFirst(); int count = cursor.getInt(0); 
+6
Feb 14 '14 at 1:06
source share

If you want to get the number of records, you need to apply the group in any field or apply the following query.

how

 db.rawQuery("select count(field) as count_record from tablename where field =" + condition, null); 
+5
Mar 05 2018-11-11T00:
source share

Another way:

 myCursor.getCount(); 

on the cursor as:

 Cursor myCursor = db.query(table_Name, new String[] { row_Username }, row_Username + " =? AND " + row_Password + " =?", new String[] { entered_Password, entered_Password }, null, null, null); 

If you can think of getting away from a raw request.

+5
Jun 18 '13 at 7:03
source share
 int nombr = 0; Cursor cursor = sqlDatabase.rawQuery("SELECT column FROM table WHERE column = Value", null); nombr = cursor.getCount(); 
+1
Sep 13 '18 at 18:10
source share
 SQLiteDatabase sqLiteDatabase = this.getReadableDatabase(); int counts = (int) DatabaseUtils.longForQuery(sqLiteDatabase, "SELECT COUNT(*) FROM "+ TABLE_NAME, null); sqLiteDatabase.close(); 
0
May 10 '19 at 4:48
source share



All Articles