1 Introduction
Android commonly used storage methods are the use of databases, mainly for single table operations, multi-table operations are not involved here.
Every time the database is used, there will be tedious code to write. The general idea is to inherit from SQLiteOpenHelper to write a database operation subclass, then rewrite onCreate method, write database table building statement, rewrite onUpgrade method, write database upgrade execution code, in addition, there are tedious add, delete, change, check statements. Every time we build a table, we have to do these repetitive tasks. Can we use a general method to hand over these tedious tasks to the program to complete automatically?
The following code will tell you the answer.
ps: In my previous blog, I also used the method of creating database tables and manipulating database tables by reflection. Although the method is ingenious, the code coupling degree before is too high, which can only be used for specific cases. Later, I learned Java generic knowledge. I can reduce the coupling degree by transferring object types through generics. In order to make my method more widely applicable, I used Java generic knowledge. In order to sort out here.
My source article http://blog.csdn.net/u012990509/article/details/53838891
2 Knowledge used
AJava reflection
BJava generic
C Database Knowledge (Transactions, Basic Statements)
3 how to use it
//Create a User table with a field name of the public variable name of the user table. You can create different tables by changing the entity type DBHelper<User> dbHelper = DBHelper.createDBHelper(this, User.class); // Delete all data dbHelper.delete(null, null); // Insert single dbHelper.insert(new User(12, "aa", true)); dbHelper.insert(new User(13, "aaffd", true)); dbHelper.insert(new User(15, "fff", false)); dbHelper.insert(new User(16, "ggggg", true)); // Query all Object object = dbHelper.querry(); System.out.println(JSON.toJSONString(object)); // Delete the entry age=12 dbHelper.delete("age=?", new String[] { "12" }); object = dbHelper.querry(); System.out.println(JSON.toJSONString(object)); // Query age=13 entries object = dbHelper.querry("age=?", new String[] { "13" }); System.out.println(JSON.toJSONString(object));
Log capture
A For querying all data
B To delete all data after age=12
C is to query only single age=13 data
4 The truth is here.
DBHelper.java
package com.example.dbtest; /** * Created by liugd on 2016/12/23. */ import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * Ratio database * * @author liugd */ public class DBHelper<T> extends SQLiteOpenHelper { private final static int DATABASE_VERSION = 1; // Database version private final static String DATABASE_NAME = "mydata.db";// Database name private final String TABLE_NAME;// Table name private DBHelperTool helperTool; private Class<T> clazz; public DBHelper(Context context, Class<T> clazz) { super(context, DATABASE_NAME, null, DATABASE_VERSION); this.TABLE_NAME = clazz.getSimpleName(); this.clazz = clazz; this.helperTool = new DBHelperTool(clazz); } public static <T> DBHelper<T> createDBHelper(Context context, Class<T> clazz) { return new DBHelper<T>(context, clazz); } @Override public void onCreate(SQLiteDatabase db) {// Create table helperTool.onCreate(db, TABLE_NAME); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME); onCreate(db);// Recreate tables } // ///////////////////// Insert data//////////////////////// /**** * Update data * * @param item * Updated entity objects */ public synchronized int update(T item, String whereClause, String[] whereArgs) { SQLiteDatabase db = this.getWritableDatabase(); db.beginTransaction(); // Manual Setup to Start Transaction int i = db.update(TABLE_NAME, helperTool.object2ContentValues(item), whereClause, whereArgs); db.setTransactionSuccessful(); // Setting the transaction to be processed successfully will automatically roll back uncommitted if not set db.endTransaction(); // Processing completed db.close(); return i; // LogUitls.PrintObject("database", "update single data"+ (i = 1)); } /*** * Insert all data * * @param list * Data to be inserted */ public synchronized long insert(T item) { long count = 0; try { SQLiteDatabase db = this.getWritableDatabase(); db.beginTransaction(); // Manual Setup to Start Transaction count = db.insert(TABLE_NAME, null, helperTool.object2ContentValues(item)); db.setTransactionSuccessful(); // Setting the transaction to be processed successfully will automatically roll back uncommitted if not set db.endTransaction(); // Processing completed db.close(); } catch (Exception e) { // LogUitls.PrintObject("database", e +"); } return count; // LogUitls.PrintObject("database", "insert all data" + count); // insert how many data } /*** * Insert all data * * @param list * Data to be inserted */ public synchronized long insert(List<T> list) { long count = 0; try { SQLiteDatabase db = this.getWritableDatabase(); db.beginTransaction(); // Manual Setup to Start Transaction for (Object item : list) {// Copy data into a new collection count += db.insert(TABLE_NAME, null, helperTool.object2ContentValues(item)); } db.setTransactionSuccessful(); // Setting the transaction to be processed successfully will automatically roll back uncommitted if not set db.endTransaction(); // Processing completed db.close(); } catch (Exception e) { // LogUitls.PrintObject("database", e +"); } return count; // LogUitls.PrintObject("database", "insert all data" + count); // insert how many data } // ////////////////////////////////////////////////////////////// // //////////////////////// Delete data//////////////////////////////// // ///////////////////////////////////////////////////////////// /** * Delete data from a specified table * * @return */ public synchronized boolean delete(String whereClause, String[] whereArgs) { // LogUitls.PrintObject("database", "delete all data"); int num = 0; try { SQLiteDatabase db = this.getWritableDatabase(); db.beginTransaction(); // Manual Setup to Start Transaction num = db.delete(TABLE_NAME, whereClause, whereArgs); db.setTransactionSuccessful(); // Setting the transaction to be processed successfully will automatically roll back uncommitted if not set db.endTransaction(); // Processing completed db.close(); } catch (Exception e) { // LogUitls.PrintObject("database", e +"); System.out.println(e + ""); } return num >= 1; } // ////////////////////////////////////////////////////////////// // //////////////////////// Query data//////////////////////////////// // ///////////////////////////////////////////////////////////// // Query single data public T querrySingle() { return querrySingle(null, null, null, null, null); } public T querrySingle(String selection, String[] selectioinArgs) { return querrySingle(selection, selectioinArgs, null, null, null); } public T querrySingle(String selection, String[] selectioinArgs, String orderBy) { return querrySingle(selection, selectioinArgs, null, null, orderBy); } /*** * Query single data * * @return */ public synchronized T querrySingle(String selection, String[] selectioinArgs, String groupBy, String having, String orderBy) { T ball = null; try { SQLiteDatabase db = this.getWritableDatabase(); db.beginTransaction(); // Manual Setup to Start Transaction Cursor cursor = db.query(TABLE_NAME, helperTool.getStringFields(), selection, selectioinArgs, groupBy, having, orderBy); if (cursor.moveToNext()) { ball = clazz.newInstance(); helperTool.setObjectData(ball, cursor); } cursor.close(); db.setTransactionSuccessful(); // Setting the transaction to be processed successfully will automatically roll back uncommitted if not set db.endTransaction(); // Processing completed db.close(); } catch (Exception e) { // LogUitls.PrintObject("database", e +"); } return ball; } // Query multiple data public List<T> querry() { return querry(null, null, null, null, null); } public List<T> querry(String selection, String[] selectioinArgs) { return querry(selection, selectioinArgs, null, null, null); } public List<T> querry(String selection, String[] selectioinArgs, String orderBy) { return querry(selection, selectioinArgs, null, null, orderBy); } /*** * Query data * * @return */ public synchronized List<T> querry(String selection, String[] selectioinArgs, String groupBy, String having, String orderBy) { List<T> list = new ArrayList<T>(); try { SQLiteDatabase db = this.getWritableDatabase(); db.beginTransaction(); // Manual Setup to Start Transaction Cursor cursor = db.query(TABLE_NAME, helperTool.getStringFields(), selection, selectioinArgs, groupBy, having, orderBy); while (cursor.moveToNext()) { T ball = clazz.newInstance(); helperTool.setObjectData(ball, cursor); list.add(ball); } cursor.close(); db.setTransactionSuccessful(); // Setting the transaction to be processed successfully will automatically roll back uncommitted if not set db.endTransaction(); // Processing completed db.close(); } catch (Exception e) { // LogUitls.PrintObject("database", e +"); } return list; } }
DBHelperTool.java
package com.example.dbtest; import java.lang.reflect.Field; import java.lang.reflect.Modifier; import java.util.ArrayList; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; /** * Database Processing Assistant, which has nothing to do with business logic, is a generic tool class Created by liugd on 2016/12/22. */ public class DBHelperTool { private String[] stringFields = null;// Static fields, automatically generated, field names of objects private Field[] flds = null;// Static fields, field properties of automatically generated entity classes public DBHelperTool(Class<?> clazz) { initAllDatas(clazz); } /*** * Initialize all fields * * @param clazz * Entity class */ private void initAllDatas(Class<?> clazz) { Field[] tmp = clazz.getFields();// Get the public fields that you have. Set the fields that do not need to create tables to private fields of entity classes. ArrayList<Field> list = new ArrayList<Field>(tmp.length); for (int i = 0; i < tmp.length; i++) { //No static variables exist when objects are serialized if (!Modifier.isStatic(tmp[i].getModifiers())) { list.add(tmp[i]); } } flds = new Field[list.size()]; stringFields = new String[flds.length]; for (int i = 0; i < flds.length; i++) { Field field = list.get(i); flds[i] = field; stringFields[i] = field.getName();// Get the name of the field } } /*** * Create data tables * * @param db * data base * @param TABLE_NAME * form */ public void onCreate(SQLiteDatabase db, String TABLE_NAME) { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("("); for (Field field : flds) { Class<?> clazz = field.getType();// Get the types of fields Intger, String, Boolean, Long // Add fields stringBuilder.append(field.getName());// Get the name of the field // Judgment type if (clazz == String.class) { stringBuilder.append(" TEXT default \"\","); } else if (clazz == int.class || clazz == boolean.class || clazz == long.class) { stringBuilder.append(" INTEGER default 0,"); } } // Remove the last comma stringBuilder.deleteCharAt(stringBuilder.length() - 1); stringBuilder.append(")"); // Create three tables String sql = "CREATE TABLE " + TABLE_NAME + stringBuilder.substring(0); db.execSQL(sql); } /*** * Set the data of the object by mapping * * @param ball * data object * @param cursor * cursor */ public void setObjectData(Object ball, Cursor cursor) { for (int i = 0; i < flds.length; i++) { try { Object object = flds[i].get(ball); if (object instanceof Integer) { // Judgement type flds[i].set(ball, cursor.getInt(i)); } else if (object instanceof String) { flds[i].set(ball, cursor.getString(i)); } else if (object instanceof Boolean) { flds[i].set(ball, cursor.getInt(i) == 1 ? true : false); } else if (object instanceof Long) { flds[i].set(ball, cursor.getLong(i)); } } catch (Exception e) { // LogUitls.PrintObject("database", "problems in value mapping when querying data"+e); } } } /** * Get all the data of the object stored in key-value pairs * * @param item * Entry data * @return Key-value pair data */ public ContentValues object2ContentValues(Object item) { ContentValues values = new ContentValues(); try { for (int i = 0; i < flds.length; i++) { String nameString = stringFields[i];// Get the field name Object object = flds[i].get(item); if (object == null) object = ""; if (object instanceof Integer) values.put(nameString, (Integer) object); else if (object instanceof String) { values.put(nameString, (String) object); } else if (object instanceof Boolean) { values.put(nameString, (Boolean) object ? 1 : 0);// True 1, false 0 } else if (object instanceof Long) { values.put(nameString, (Long) object); } } } catch (Exception e) { // LogUitls.PrintObject("Database", "Problems with Value Mapping"+e); } return values;// Return key-value pairs } // GET SET METHOD public Field[] getFlds() { return flds; } public void setFlds(Field[] flds) { this.flds = flds; } public String[] getStringFields() { return stringFields; } public void setStringFields(String[] stringFields) { this.stringFields = stringFields; } }
2017.3.12
If you have any questions, you are welcome to discuss them.
http://download.csdn.net/detail/u012990509/9778252