1、继承OrmLiteSqliteOpenHelper:
public
class SQLiteHelperOrm
extends OrmLiteSqliteOpenHelper {
public static final String DATABASE_PATH = Environment
.getExternalStorageDirectory() + "/test.db";
@Override
public synchronized SQLiteDatabase getWritableDatabase() {
return SQLiteDatabase.openDatabase(DATABASE_PATH, null,
SQLiteDatabase.OPEN_READWRITE);
}
public synchronized SQLiteDatabase getReadableDatabase() {
return SQLiteDatabase.openDatabase(DATABASE_PATH, null,
SQLiteDatabase.OPEN_READONLY);
}
public static final String DATABASE_PATH = Environment
.getExternalStorageDirectory() + "/test.db";
@Override
public synchronized SQLiteDatabase getWritableDatabase() {
return SQLiteDatabase.openDatabase(DATABASE_PATH, null,
SQLiteDatabase.OPEN_READWRITE);
}
public synchronized SQLiteDatabase getReadableDatabase() {
return SQLiteDatabase.openDatabase(DATABASE_PATH, null,
SQLiteDatabase.OPEN_READONLY);
}
代码说明:
注意这里覆盖了getWritableDatabase和getReadDatabase,并且没有super。
2、Application里面创建数据库
File f =
new File(SQLiteHelperOrm.DATABASE_PATH);
if (!f.exists()) {
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(
SQLiteHelperOrm.DATABASE_PATH, null);
SQLiteHelperOrm orm = new SQLiteHelperOrm( this);
orm.onCreate(db);
db.close();
}
if (!f.exists()) {
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(
SQLiteHelperOrm.DATABASE_PATH, null);
SQLiteHelperOrm orm = new SQLiteHelperOrm( this);
orm.onCreate(db);
db.close();
}
注意close掉数据库。
本文转自博客园农民伯伯的博客,原文链接:ormlite更改数据库默认位置,如需转载请自行联系原博主。