Source: download.csdn.net/detail/a924…
First, frame efficiency comparison
ORMLite ARUD package
3. Database backup and recovery encapsulation
Examples: bru.doDataBackUp(DATABASE_PATH, DB_BACKUP_PATH, New DataBaseUtils. MessageShow () {@ Override public void onPepare () {Toast. MakeText (MainActivity. This, "database backup!" , Toast.LENGTH_LONG).show(); } @override public void onSuccess() {toast.maketext (mainactivity.this, "database backup succeeded!" , Toast.LENGTH_LONG).show(); } @override public void onFail() {toast.maketext (mainactivity.this, "database backup failed! , Toast.LENGTH_LONG).show(); }});Copy the code
Call doDataRecover(String DB_PATH, String DB_BACKUP_PATH, MessageShow MS) on DataBaseUtils.
bru.doDataRecover(DATABASE_PATH, DB_BACKUP_PATH, New DataBaseUtils. MessageShow () {@ Override public void onPepare () {Toast. MakeText (MainActivity. This database recovery! "" , Toast.LENGTH_LONG).show(); } @override public void onSuccess() {toast.maketext (mainactivity.this, "database restored successfully!" , Toast.LENGTH_LONG).show(); } @override public void onFail() {toast.maketext (mainactivity.this, "database restore failed!" , Toast.LENGTH_LONG).show(); }});Copy the code
However, after you change the database creation address, you need to change the database version number in this method rather than in DatabaseHelper during subsequent version update operations to upgrade the database version.
Used, for example: DATABASE_PATH = Environment. External.getexternalstoragedirectory () + "/ kktest. Db"; MyDatabaseHelper helper = new MyDatabaseHelper(MainActivity.this, DATABASE_PATH); DataBaseUtils bru=new DataBaseUtils(MainActivity.this); int dataBaseVersion=1; bru.setDatabasePath(helper, DATABASE_PATH, dataBaseVersion);Copy the code
4. Ormlite-based databases are constantly updated
Custom database creation address method :(must be called when the program starts)
Add the following code to the application or Activity entry:
DataBaseUtils bru =new DataBaseUtils(MainActivity.this);
MyDatabaseHelper helper= new MyDatabaseHelper(
MainActivity.this,DATABASE_PATH);
bru.setDatabasePath(helper, DATABASE_PATH,versionCode);
Specifies the location where the database is created and the current database version. You need to change the database version when the database version changes.
Since the getWritableDatabase() and getReadableDatabase() methods are overridden in the DatabaseHelper base class, the onCreate() and onUpgrade () methods will not be automatically called when the database is created and updated. This needs to be called in the Application or Activity when the program starts
SetDatabasePath method
The onCreate() and onUpgrade () methods are called in this method by specifying the database save location.