android - SQLite: Obtaining total of all values in one column -
I want to create a query that will give the total values in a single column of my SQLite datase.
I want this query to be within a method that returns completely as an intern so that I can move it forward in any other activity.
How can I do this?
I have already created similar methods within my database assistant section (see below), but I do not know how to implement a query inside SQLite to give the total
/ Database Version Personal Static Final End DATABASE_VERSION = 10; // Database Name Private Last Fixed String DATABASE_NAME = "Attention Database"; // Contact table name personal constant last string TABLE_SCORE = "score"; // contact table column name personal constant last string COL_SESSION = "sessionid"; Private static last string COL_GAMETITLE = "sports"; Private static last string COL_NAME = "name"; Private static last string COL_MED = "avgmeditation"; Private static final string COL_MAX = "max minutes"; Private static final string COL_AVGATT = "avgattention"; Private static final string COL_MAXATT = "maximum"; Private static final string COL_SCORE = "score"; Private static final string COL_DATE = "date"; / ** * Constructor * * @ Ultimate Context * / Public Database Helper (Reference Reference) {Super (Reference, DatabaseNN, Blank, DatabaseBens); } / ** * method that creates the database / / override the creation of the public at zero (SQLiteDatabase DB) {string CREATE_TABLE_SCORE = "create table" + TABLE_SCORE + "(" + COL_SESSION + "STRING primary key," + COL_GAMETITLE + "STRING, "+ COL_NAME +" INTEGER, "+ COL_MAX +" INTEGER, "+ COL_AVGATT +" INTEGER, "+ COL_MAXATT +" INTEGER, "+ COL_SCORE +" INTEGER, "+ COL_DATE +" STRING "+") "; Db.execSQL (CREATE_TABLE_SCORE); } Public Class DatabaseHelper Extends SQLiteOpenHelper {/ p>
Potentially similar method that returns the number of entries in DB:
public int getTotalGamesPlayed () {SQLiteDatabase db = this.getWritableDatabase () ; {Try Returning (int) DatabaseUtils.queryNumEntries (DB, TABLE_SCORE); } Finally {db.close (); }}
Edit:
Is this the correct query:
string query = "SELECT SUM (COL_MED) FROM" + TABLE_SCORE;
read; You can either use sum ()
or total ()
.
The column name is not COL_MED
; This is the name of the symbol whose value is the name of the column, like TABLE_SCORE
, you must enter its value in the SQL query:
Int getSumOfAllAvgMeditations () {SQLiteDatabase db = getWritableDatabase (); Select {String sql = "total (" + COL_MED + ")" + TABLE_SCORE; Return (int) DatabaseUtils.longForQuery (db, sql, blank); } Finally {db.close (); }}
Comments
Post a Comment