In Android databases, the SQLiteOpenHelper class is inherited first. The following code is directly pasted:
Public class DBHelper extends SQLiteOpenHelper {
/**
* @ Fields name: Database name
*/
Private final static String name = "info. db ";
/**
* @ Fields version: version Number
*/
Private final static int version = 1;
Private Class <?> [] Clazzs = new Class <?> [] {DemoBean. class}; // object class of the associated database
Public DBHelper (Context context, String name, CursorFactory factory,
Int version ){
Super (context, name, factory, version );
}
Public DBHelper (Context context ){
Super (context, name, null, version );
}
/*
* Used to create a table in the database <p> Title: onCreate </p> <p> Description: </p>
* @ Param db
* @ See
* Android. database. sqlite. SQLiteOpenHelper # onCreate (android. database. sqlite
*. SQLiteDatabase)
*/
@ Override
Public void onCreate (SQLiteDatabase db ){
For (int I = 0; I <clazzs. length; I ++ ){
DBUtils. createTable (db, clazzs [I]);
}
}
/*
* This method is called after the database version is changed. <p> Title: onUpgrade </p> <p> Description: </p>
*
* @ Param db
*
* @ Param oldVersion
*
* @ Param newVersion
*
* @ See
* Android. database. sqlite. SQLiteOpenHelper # onUpgrade (android. database. sqlite
*. SQLiteDatabase, int, int)
*/
@ Override
Public void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion ){
}
}
DBUtils. createTable (db, clazzs [I]); this static method is mainly used to automatically generate SQL statements (because I do not like to write SQL statements most, java reflection technology ). Java Annotation is used here. If you do not know it, you can use Baidu ).
In addition, database operations are nothing more than adding, deleting, modifying, and querying-> in order not to add, deleting, modifying, and querying methods for each table, all the generic methods are used here, but note that: the class name and field name in the JavaBeen must be the same as the field structure in the database table. Note that the interface is not used as much as possible and I don't think it is necessary.
Because the code is too long: I only paste the insert method:
Public <T> void insertData (T t) throws NumberFormatException,
IllegalArgumentException, IllegalAccessException {
SQLiteDatabase db = null;
Synchronized (DriverManager. class ){
Db = helper. getWritableDatabase ();
}
Field [] fields = t. getClass (). getDeclaredFields (); // use violence
ContentValues values = new ContentValues ();
For (Field field: fields ){
// Allow access to private variables
Field. setAccessible (true );
If ("double". equals (field. getType (). toString ())){
Values. put (field. getName (),
Double. valueOf (String. valueOf (field. get (field ))));
}
If ("int". equals (field. getType (). toString ())){
Values. put (field. getName (),
Integer. valueOf (String. valueOf (field. get (field ))));
}
If ("float". equals (field. getType (). toString ())){
Values. put (field. getName (),
Float. valueOf (String. valueOf (field. get (field ))));
}
If ("class java. lang. String". equals (field. getType (). toString ())){
Values. put (field. getName (), String. valueOf (field. get (field )));
}
}
Db. insert (t. getClass (). getSimpleName (), null, values );
Db. close ();
}
Now I have written so much for the time being. I will write the language for the next time. What should I do for the first time !!