android - SQLite sorting by Fields -
i newbie sqlite , don't know how sort fields. searched use "order by", cannot well. have class implement sqlite functions follow:
public class plsqliteopenhelper extends sqliteopenhelper{ public string tablenames[]; public string fieldnames[][]; public string fieldtypes[][]; public static string no_create_tables = "no tables"; private string message = ""; public plsqliteopenhelper(context context, string name, cursorfactory factory, int version, string tablenames[], string fieldnames[][], string fieldtypes[][]) { super(context, name, factory, version); tablenames = tablenames; fieldnames = fieldnames; fieldtypes = fieldtypes; } @override public void oncreate(sqlitedatabase db) { if(tablenames == null){ message = no_create_tables; return; } for(int = 0; < tablenames.length; i++){ string sql = "create table " + tablenames[i] + "("; for(int j = 0; j < fieldnames[i].length; j++) sql += fieldnames[i][j] + " " + fieldtypes[i][j] + ","; sql = sql.substring(0, sql.length() - 1); sql += ")"; db.execsql(sql); } } @override public void onupgrade(sqlitedatabase db, int oldversion, int newversion) { for(int = 0; < tablenames[i].length(); i++){ string sql = "drop table if exists " + tablenames[i]; db.execsql(sql); } oncreate(db); } public void execsql(string sql) throws java.sql.sqlexception{ sqlitedatabase db = this.getwritabledatabase(); db.execsql(sql); } public cursor select(string table, string[] columns, string selection, string[] selectionargs, string groupby, string having, string orderby){ sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table, columns, selection, selectionargs, groupby, having, orderby); return cursor; } public long insert(string table, string fields[], string values[]){ sqlitedatabase db = this.getwritabledatabase(); contentvalues cv = new contentvalues(); for(int = 0; < fields.length; i++) cv.put(fields[i], values[i]); return db.insert(table, null, cv); } public int sort(string table, string updatefields[], string orderby){ sqlitedatabase db = this.getreadabledatabase(); cursor cursor = db.query(table, updatefields, null, null, null, null, orderby); contentvalues cv = new contentvalues(); int = 0; while(cursor.movetonext()){ cv.putnull(updatefields[i]); i++; } return db.update(table, cv, null, null); } public int delete(string table, string where, string[] wherevalue){ sqlitedatabase db = this.getwritabledatabase(); return db.delete(table, where, wherevalue); } public int update(string table, string updatefields[], string updatevalues[], string where, string[] wherevalue){ sqlitedatabase db = this.getwritabledatabase(); contentvalues cv = new contentvalues(); for(int = 0; < updatefields.length; i++) cv.put(updatefields[i], updatevalues[i]); return db.update(table, cv, where, wherevalue); } public string getmessage(){ return message; } @override public synchronized void close(){ super.close(); } }
i know need use "select" & "update" function together, don't know how call them in order sort table field name. can tell me function call order follow information?
table names: "t_passwordlist" field names: "f_id", "f_service", "f_user", "f_pwd", "f_lanuch" field types: "integer primary key autoincrement", "text", "text", "text", "text" order by: "f_service"
create object of helper class , call method way.
cursor cursor = plsqliteopenhelper.select("table_name", new string[] { "f_id", "f_service", "f_user", "f_pwd", "f_lanuch" }, null, null, null, null, "f_service");
hope helps.
Comments
Post a Comment