Trying to get a count of the rows in a database from a separate activity. I have been messing with the methods to do so, but I can't seem to instantiate them from another activity if the method is built in the database adapter class. Ideally, I would like the method in the adapter to return an int that I can then use in a number of different activities down the chain. Is there a way to do this that I am not thinking of?
views:
44answers:
1
+2
A:
You should make use of the DatabaseUtils.stringForQuery() static method that is already in Android SDK to easily retrieve a value, this example is for String
bot there is also method for Long
stringForQuery(SQLiteDatabase db, String query, String[] selectionArgs)
Utility method to run the query on the db and return the value in the first column of the first row.
Something like
String myString=DatabaseUtils.stringForQuery(getDB(),query,selectionArgs);
Pentium10
2010-09-27 20:00:24
I don't think you understood the question. I want to count the total rows in the database and assign that number to an int so that I can pass it around to different activities and do different things with it.
Will
2010-09-28 00:25:43
You either could use a "select count(*) from table" query and get the result with the static call of `longForQuery`, or if you've checked the DatabaseUtils static methods you will also find a `queryNumEntries` method, that you can use. It returns a long.
Pentium10
2010-09-28 05:18:02
You also probably store your DB object reference somewhere static in your model, probably Application class. So it will be simple to get the reference and use in any activity classes you want. I am 100% this is the good way to go. The others are too complicated, and distract with code, where you can fail, or introduce errors.
Pentium10
2010-09-28 05:19:51
Alright, so I have to do some research on the databaseUtils - I have never even heard of those. As you can tell, no doubt, I am really new to java and android. I will start work on researching that part of Android next. Thanks for your patience on this. :)
Will
2010-09-28 13:01:52
longForQuerypublic static long longForQuery(SQLiteStatement prog, java.lang.String[] selectionArgs) So, I would build this in the adapter class, but I have no idea what SQLiteStatement prog is...gotta keep digging apparently.
Will
2010-09-28 13:13:44
So, something more like this, added as a method in the adapter class? public static long queryNumEntries(SQLiteDatabase db, String table, String selection, String[] selectionArgs) { String s = (!TextUtils.isEmpty(selection)) ? " where " + selection : ""; return longForQuery(db, "select count(*) from " + table + s, selectionArgs); } My only problem is, I am not sure what to put in the string s = part. Should that be the primary key, the "_id"
Will
2010-09-28 15:47:42
No. You don't have to do those methods. Those already exists. You should use them, somehow like `long myCount=DatabaseUtils.queryNumEntries(getDb(),"mytablename");` As you see you just have to get the db object with some method, you need to figure out in your model.
Pentium10
2010-09-28 16:22:39
Right on, thanks for your help with this. I am at work now, but I will try it the minute I get home and post back.
Will
2010-09-28 17:06:41
Alright, I can not seem to get this thing to work, hopefully you can point out what I am doing wrong. In my adapter class, I have this method
Will
2010-09-29 01:21:53
public long getCount(SQLiteDatabase db){ mDbHelper = new DatabaseHelper(mCtx); db = mDbHelper.getReadableDatabase(); long myCount=DatabaseUtils.queryNumEntries(db,DATABASE_TABLE); return myCount; }
Will
2010-09-29 01:23:52
Then in my activity I make a call like this number = mDbHelper.getCount(mDbHelper.mDb);
Will
2010-09-29 01:24:29
Oh, crap, it does work, I uninstalled the app and therefore the stupid database was empty and I kept trying to count the rows and it kept saying zero, and I then I realized, it was zero! LOL. Pentium 10, you the man! Thank you so much for pointing me in the right direction!
Will
2010-09-29 01:29:23