First, let's talk about the following test code. It's a bit messy. We should pay attention to it. The following examples are all the framework of the ORM Lite.
First, customize the location of the database:
It is suggested that a class DatabaseHelper inherit OrmLiteSqliteOpenHelper, and then rewrite getWritableDatabase, getReadableDatabase method.
@Override public synchronized SQLiteDatabase getWritableDatabase() { LogUtil.e(DatabaseHelper.class.getName(), "getWritableDatabase()"); /*super.getWritableDatabase();*/ return SQLiteDatabase.openDatabase(DATABASE_PATH, null, SQLiteDatabase.OPEN_READWRITE); } @Override public synchronized SQLiteDatabase getReadableDatabase() { LogUtil.e(DatabaseHelper.class.getName(), "getReadableDatabase()"); /*super.getReadableDatabase();*/ return SQLiteDatabase.openDatabase(DATABASE_PATH, null, SQLiteDatabase.OPEN_READONLY); }
Notice that I've blocked super's method here.
The second step is to write in app:
File f = new File(DatabaseHelper.DATABASE_PATH); if (!f.exists()) { LogUtil.e(TAG, "!f.exists()"); if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED) == true) { LogUtil.e(TAG, "Environment.MEDIA_MOUNTED == true"); File f1 = new File(Environment.getExternalStorageDirectory() + "/timetask/database"); f1.mkdirs(); SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase( DatabaseHelper.DATABASE_PATH, null); DatabaseHelper orm; try { orm = new DatabaseHelper(this); orm.onCreate(db); db.close(); } catch (NameNotFoundException e) { e.printStackTrace(); } } }
Used to create a custom library for the first time.
In this way, you have successfully created a custom library. However, the problem has not come up. The super.getWritableDatabase() method is not invoked in getWritableDatabase, and super.getReadableDatabase() is invoked in getReadableDatabase. It will find that there is no onCreate and onUpgrade method in DatabaseHelper at all. This is a problem that can not automatically update the database. Then I opened super and an error occurred:
12-12 16:54:58.240: E/SQLiteLog(15354): (1) table `simpledata` already exists 12-12 16:54:58.260: E/com.example.ormlitebdtest.DatabaseHelper(15354): Can't create database 12-12 16:54:58.260: E/com.example.ormlitebdtest.DatabaseHelper(15354): java.sql.SQLException: SQL statement failed: CREATE TABLE `simpledata` (`date` VARCHAR , `string` VARCHAR , `millis` BIGINT , `id` INTEGER PRIMARY KEY AUTOINCREMENT , `even` SMALLINT ) 12-12 16:54:58.260: E/com.example.ormlitebdtest.DatabaseHelper(15354): at com.j256.ormlite.misc.SqlExceptionUtil.create(SqlExceptionUtil.java:22) 12-12 16:54:58.260: E/com.example.ormlitebdtest.DatabaseHelper(15354): at com.j256.ormlite.table.TableUtils.doStatements(TableUtils.java:464) 12-12 16:54:58.260: E/com.example.ormlitebdtest.DatabaseHelper(15354): at com.j256.ormlite.table.TableUtils.doCreateTable(TableUtils.java:440) 12-12 16:54:58.260: E/com.example.ormlitebdtest.DatabaseHelper(15354): at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:220) 12-12 16:54:58.260: E/com.example.ormlitebdtest.DatabaseHelper(15354): at com.j256.ormlite.table.TableUtils.createTable(TableUtils.java:53) 12-12 16:54:58.260: E/com.example.ormlitebdtest.DatabaseHelper(15354): at com.example.ormlitebdtest.DatabaseHelper.onCreate(DatabaseHelper.java:30) 12-12 16:54:58.260: E/com.example.ormlitebdtest.DatabaseHelper(15354): at com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper.onCreate(OrmLiteSqliteOpenHelper.java:207) 12-12 16:54:58.260: E/com.example.ormlitebdtest.DatabaseHelper(15354): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
If you call the table that has been created again, an error will be reported.
I had to close the super method and call onUpgrade manually, so I wrote in the app class
else{ LogUtil.e(TAG, "f.exists()"); if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED) == true) { LogUtil.e(TAG, "Environment.MEDIA_MOUNTED == true"); SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase( DatabaseHelper.DATABASE_PATH, null); DatabaseHelper orm; try { orm = new DatabaseHelper(this); orm.onUpgrade(db, db.getVersion(), getPackageManager().getPackageInfo(getPackageName(), 0).versionCode); db.close(); } catch (NameNotFoundException e) { e.printStackTrace(); } }
Call manually. The printed log is as follows
The update method is called.
I don't know if it's a bug. I hope you can give me some suggestions. Thank you.
For questions about db location, please refer to: http://stackoverflow.com/questions/6629021/android-orm-db-location/13111761.
(I don't know how to upload the code. Is my permission insufficient? Download address: Click to open the link)
Reproduced at: https://www.cnblogs.com/riasky/p/3473504.html