java - Android Database SQLite Average -
i making app android sqlite database have 1 table , 2 columns: 1 names , other marks. also, can see information of database in listview , can add more elements it. how can make average of marks in database? , how can delete row?
i paste database helper
public class persondatabasehelper {
private static final string tag = persondatabasehelper.class.getsimplename(); // database configuration // if want onupgrade run change database_version private static final int database_version = 1; private static final string database_name = "mydatabase.db"; // table configuration private static final string table_name = "person_table"; // table name private static final string person_table_column_id = "_id"; // column named "_id" required cursor private static final string person_table_column_name = "person_name"; private static final string person_table_column_pin = "person_pin"; private databaseopenhelper openhelper; private sqlitedatabase database; // wrapper class. means, outside world, communicate persondatabasehelper, // under hood databaseopenhelper class perform database crud operations public persondatabasehelper(context acontext) { openhelper = new databaseopenhelper(acontext); database = openhelper.getwritabledatabase(); } public void insertdata (string apersonname, string apersonpin) { // using contentvalues avoid sql format errors contentvalues contentvalues = new contentvalues(); contentvalues.put(person_table_column_name, apersonname); contentvalues.put(person_table_column_pin, apersonpin); database.insert(table_name, null, contentvalues); } public cursor getalldata () { string buildsql = "select * " + table_name; log.d(tag, "getalldata sql: " + buildsql); return database.rawquery(buildsql, null); } // databaseopenhelper class used perform database related operation private class databaseopenhelper extends sqliteopenhelper { public databaseopenhelper(context acontext) { super(acontext, database_name, null, database_version); } @override public void oncreate(sqlitedatabase sqlitedatabase) { // create tables here string buildsql = "create table " + table_name + "( " + person_table_column_id + " integer primary key, " + person_table_column_name + " text, " + person_table_column_pin + " text )"; log.d(tag, "oncreate sql: " + buildsql); sqlitedatabase.execsql(buildsql); } @override public void onupgrade(sqlitedatabase sqlitedatabase, int oldversion, int newversion) { // database schema upgrade code goes here string buildsql = "drop table if exists " + table_name; log.d(tag, "onupgrade sql: " + buildsql); sqlitedatabase.execsql(buildsql); // drop previous table oncreate(sqlitedatabase); // create table beginning } }
}
use avarage (avg) aggregate function:
string query = "select avg("+person_table_column_pin +") "+table_name;
and use sqlitedatabase.rawquery(string sql, string[] selectionargs) executing query. like:
database.rawquery(query, null);
here can find sample fiddle.
while, deleting row, can use sqlitedatabase.delete(string table, string whereclause, string[] whereargs). example:
string = person_table_column_id+"=?"; string[] whereargs = new string[]{string.valueof(59)}; database.delete(table_name, where, whereargs);
the above code delete row id 59.
Comments
Post a Comment