============ Problem Description ============
I'm a rookie recently learning something about the Android database there's a problem I don't know how to solve
For example, there is now a table a,a with four fields q,w,e,r. where e is of type int, now to get the sum of all e in the table, how to write the code. Is database, is it using database.rawquery () This method?
Ask the great God for advice!
============ Solution 1============
int sum (Sqlitedatabase db) {cursor cursor = db.rawquery ("Select SUM (e) from a", null); Cursor.movetonext (); int sum = cursor . getInt (0); Cursor.close (); return sum;}
============ Solution 2============
Upstairs, but I think it's better.
int sum () {Sqlitedatabase db = Dbmanger.getinstance (); cursor cursor = db.rawquery ("Select SUM (e) from a", null); Cursor.movetonext (); int sum = cursor.getint (0); Cursor.close (); Db.close (); return sum;}
Once the database is in use, close it immediately to avoid unnecessary exception collisions. Dbmanger.getinstance (); Is the way you get a DB instance
Questions about SQL statements