Android - 帮助从内置 SQLite 迁移到 Ormlite
Android - HELP moving away from builtin SQLite to Ormlite
我在 Android 中创建了一个 SQLite database
,我正在手动管理所有代码以执行读取和写入。我最近发现了 ORMlite
。从现在开始,我想使用 ORMlite
来管理我的数据库。问题是应用程序已经在 android 市场上,我不希望我的用户丢失他们的数据。
有什么方法可以告诉 ORMlite
开始管理已经创建的数据库吗?或者是否有从旧数据库读取所有数据并将其写入新数据库的标准做法?
在进行了大量尽职调查之后,我意识到这是一项多么简单的任务。 Ormlite
实际上位于内置 SQLite 之上。无需代码即可移至 Ormlite
。我只是在 Ormlite Helper Class
中引用了我的数据库名称。
我的代码如下。我希望这对将来的其他人有帮助。
public class OrmHelper extends OrmLiteSqliteOpenHelper {
private final String TAG = this.getClass().getSimpleName();
private Context context;
public OrmHelper(Context context) {
//references my Sqlite dbnames. I made them static in the SqlHelper class
super(context, DataBase.DB_Name, null, DataBase.DB_Version);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
Log.i(TAG, "Creating database in Ormlite");
TableUtils.createTable(connectionSource, Model.class);
TableUtils.createTable(connectionSource, UserCredential.class);
} catch (SQLException e) {
Log.e(TAG, "Error creating database", e);
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource,
int oldVersion, int newVersion) {
}
/**
* this genric method is for grabbing the Dao for any ormlite table
*/
public <T, V> Dao<T, V> getTypeDao(Class<T> classType, Class<V> idType)
throws SQLException{
return getDao(classType);
}
}
我在 Android 中创建了一个 SQLite database
,我正在手动管理所有代码以执行读取和写入。我最近发现了 ORMlite
。从现在开始,我想使用 ORMlite
来管理我的数据库。问题是应用程序已经在 android 市场上,我不希望我的用户丢失他们的数据。
有什么方法可以告诉 ORMlite
开始管理已经创建的数据库吗?或者是否有从旧数据库读取所有数据并将其写入新数据库的标准做法?
在进行了大量尽职调查之后,我意识到这是一项多么简单的任务。 Ormlite
实际上位于内置 SQLite 之上。无需代码即可移至 Ormlite
。我只是在 Ormlite Helper Class
中引用了我的数据库名称。
我的代码如下。我希望这对将来的其他人有帮助。
public class OrmHelper extends OrmLiteSqliteOpenHelper {
private final String TAG = this.getClass().getSimpleName();
private Context context;
public OrmHelper(Context context) {
//references my Sqlite dbnames. I made them static in the SqlHelper class
super(context, DataBase.DB_Name, null, DataBase.DB_Version);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) {
try {
Log.i(TAG, "Creating database in Ormlite");
TableUtils.createTable(connectionSource, Model.class);
TableUtils.createTable(connectionSource, UserCredential.class);
} catch (SQLException e) {
Log.e(TAG, "Error creating database", e);
}
}
@Override
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource,
int oldVersion, int newVersion) {
}
/**
* this genric method is for grabbing the Dao for any ormlite table
*/
public <T, V> Dao<T, V> getTypeDao(Class<T> classType, Class<V> idType)
throws SQLException{
return getDao(classType);
}
}