先说说以下为测试代码,有点乱,大家讲究着看。以下例子都是采用的ormlite的框架。
第一步,自定义数据库的位置:
建议一个类DatabaseHelper 继承 OrmLiteSqliteOpenHelper,然后重写getWritableDatabase,getReadableDatabase方法
@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); }
第二步在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(); } } }用来初次创建自定义的库。
这样就已经可以成功创建了自定义的库,但是问题来了,没有在getWritableDatabase中调用super.getWritableDatabase()方法,和getReadableDatabase中调用super.getReadableDatabase(),会发现根本就没有走DatabaseHelper中的onCreate和onUpgrade方法,这样就照成了一个问题,无法自动更新数据库。然后我打开了super,出现了这样的错误:
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)已经创建了的表,再次调用会报错。
我只有关闭了super的方法,进行手动调用onUpgrade,于是在app的类中写了
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(); } }
这样就调用了更新方法。
不知道这个方式是不是挺bug的,希望大家提提意见和建议,谢谢了。
关于db location的问题参考了:http://stackoverflow.com/questions/6629021/android-ormlite-db-location/13111761#13111761
(不知道怎么上传代码了,是不是我的权限不够啊,下载地址:点击打开链接)