In view of the frequent use of Sqlite database for data persistence, a little encapsulation, easy to use.
This package class mainly supports the function
- Supports multi-user data storage
- Support for SQLite database upgrade
- Support for incoming SQL statement build table
- Support Sqlitedatabase basic operation. For example: Execsql, rawquery, insert, etc.
- Resolves a database concurrency problem
Affix the package class code first
/** * * @ClassName: Databaseopenhelper * @Description: Database Tool class * @author Lhy * @date 2014-10-9 PM 2:36:41 * */public class Databaseopenhelper extends Sqliteopenhelper {private static map<string, databaseopenhelper> dbmaps = new HASHMAP&L T String, databaseopenhelper> ();p rivate onsqliteupdatelistener onsqliteupdatelistener;/** * Table statement list */private list <String> createtablelist;private String Nowdbname;private databaseopenhelper (context context, string dbName, int Dbversion, list<string> Tablesqls) {Super (context, dbName, NULL, dbversion); nowdbname = Dbname;createtablelist = New Arraylist<string> (); Createtablelist.addall (TABLESQLS);} /** * * @Title: getinstance * @Description: Get DB Instance * @param @param context * @param @param userId * @param @return * @re Turn Databaseopenhelper * @author lihy */public static Databaseopenhelper getinstance (context context, String dbName, int Dbversion, list<string> tablesqls) {Databaseopenhelper databaseopenhelper = Dbmaps.get (dbname); if (databaseopenhelper = = null) {Databaseopenhelper = new Databaseopenhelper (context, DbName, Dbversion, TABLESQLS);} Dbmaps.put (DbName, databaseopenhelper); return databaseopenhelper;}; @Overridepublic void OnCreate (Sqlitedatabase db) {for (String sqlstring:createtablelist) {db.execsql (sqlString);}} /** * * @Title: Execsql * @Description: SQL write * @param @param SQL * @param @param bindargs * @return void * @author Lihy */public void Execsql (String sql, object[] bindargs) {Databaseopenhelper databaseopenhelper = Dbmaps.get (nowDbName); Synchronized (databaseopenhelper) {sqlitedatabase database = Databaseopenhelper.getwritabledatabase (); Database.execsql (SQL, Bindargs);}} /** * * @Title: Rawquery * @Description: * @param @param SQL query * @param @param bindargs * @param @return * @return Cursor * @author Lihy */public Cursor rawquery (String sql, string[] bindargs) {Databaseopenhelper Databaseopenhelper = dbmaps.ge T (nowdbname); synchronized (databaseopenhelper) {sqlitedatabase database = dAtabaseopenhelper.getreadabledatabase (); cursor cursor = database.rawquery (sql, Bindargs); return cursor;}} /** * * @Title: Insert * @Description: Inserts data * @param @param table * @param @param contentvalues set file * @return void return type * @author Lihy * @throws */public void Insert (String table, Contentvalues contentvalues) {databaseopenhelper databaseopenh Elper = Dbmaps.get (nowdbname); synchronized (databaseopenhelper) {Sqlitedatabase database = Databaseopenhelper.getwritabledatabase ();d atabase.insert (table, NULL, contentvalues);}} /** * * @Title: UPDATE * @Description: Updates * @param @param table * @param @param values * @param @param whereclause * @par Am @param whereargs settings file * @return void return type * @throws */public void Update (string table, contentvalues values, string whe Reclause, string[] whereargs) {Databaseopenhelper databaseopenhelper = Dbmaps.get (nowdbname); synchronized ( Databaseopenhelper) {Sqlitedatabase database = Databaseopenhelper.getwritabledatabase ();d atabase.update (table, Values, wheReclause, Whereargs);}} /** * * @Title: Delete * @Description: Remove * @param @param table * @param @param whereclause * @param @param whereargs * @r eturn void * @author lihy */public void Delete (string table, String Whereclause, string[] whereargs) {Databaseopenhelper D Atabaseopenhelper = Dbmaps.get (nowdbname); synchronized (databaseopenhelper) {Sqlitedatabase database = Databaseopenhelper.getwritabledatabase ();d atabase.delete (table, Whereclause, Whereargs);}} /** * * @Title: Query * @Description: Check * @param @param table * @param @param columns * @param @param selection * @param @param selectionargs * @param @param groupBy * @param @param have * @param @param * * @return void * @author Lihy */public Cursor Query (string table, string[] columns, string selection, string[] Selectionargs, string groupBy, string hav ing,string) {Databaseopenhelper databaseopenhelper = Dbmaps.get (nowdbname); synchronized (Databaseopenhelper) {Sqlitedatabase Database = databaseopenhelper.getreadableDatabase ();//cursor cursor = database.rawquery ("SELECT * from"//+ Tablename.table_name_user + "where userId =" + Useri D, NULL); cursor cursor = database.query (table, columns, selection, Selectionargs, GroupBy, having, as-is); return cursor;}} /** * * @Description: Check * @param table * @param columns * @param selection * @param selectionargs * @param groupBy * @para M having * @param by * @param limit * @return * Cursor * @exception: * @author: Lihy * @time: 2015-4-3 a.m. 9:37:29 */pub Lic Cursor query (string table, string[] columns, string selection, string[] Selectionargs, String groupBy, String having,s Tring orderby,string limit) {Databaseopenhelper Databaseopenhelper = Dbmaps.get (nowdbname); synchronized ( Databaseopenhelper) {Sqlitedatabase database = Databaseopenhelper.getreadabledatabase ();//CURSOR cursor = Database.rawquery ("SELECT * from"//+ Tablename.table_name_user + "where userId =" + userid, NULL); cursor cursor = database.query (table, columns, selection, Selectionargs, GROupby, having, by-do, limit); return cursor;}} /** * * @Description query, method overload, table table name, sqlstring condition * @param @return * @return cursor * @author lihy */public cursor query (S Tring TableName, String sqlString) {Databaseopenhelper databaseopenhelper = Dbmaps.get (nowdbname); synchronized ( Databaseopenhelper) {Sqlitedatabase database = Databaseopenhelper.getreadabledatabase (); cursor cursor = database.rawquery ("SELECT * from" + TableName + "" + sqlString, NULL); return cursor;}} /** * @see android.database.sqlite.sqliteopenhelper#close () */public void Clear () {Databaseopenhelper Databaseopenhelper = Dbmaps.get (nowdbname);d atabaseopenhelper.close ();d bmaps.remove (databaseopenhelper);} The/** * Onupgrade () method deletes the database table on the user's phone each time the database version changes, and then re-creates it. <br/> * It is generally not possible to do this in real-world projects, the correct way is to update the database table structure, but also to consider the user's data stored in the database is not lost, from version to release a few updates. (Non * Javadoc) * * @see Android.database.sqlite.sqliteopenhelper#onupgrade (Android.database.sqlite *). sqlitedatabase, int, int) */@Overridepublic void Onupgrade (Sqlitedatabase db,int arg1, int arg2) {if (Onsqliteupdatelistener! = null) {Onsqliteupdatelistener.onsqliteupdatelistener (db, Arg1, arg2);} }public void Setonsqliteupdatelistener (Onsqliteupdatelistener onsqliteupdatelistener) {This.onSqliteUpdateListener = Onsqliteupdatelistener;}}
Database Update Interface Code
Public interface Onsqliteupdatelistener {public void Onsqliteupdatelistener (sqlitedatabase db, int oldversion, int newversion);}
Android Sqlite Tool Class Package