Android - HELP从内置SQLite转移到Ormlite

时间:2021-03-08 19:23:40

I have an SQLite database that I created in Android that I'm manually managing all the code for to perform my reading and writing. I recently discovered ORMlite. I want to use ORMlite to manage my database from this point forward. The issue is the application is already on the android market and I don't want my user's to lose their data.

我有一个我在Android中创建的SQLite数据库,我手动管理所有代码以执行我的阅读和写作。我最近发现了ORMlite。我想从现在开始使用ORMlite来管理我的数据库。问题是应用程序已经在Android市场上,我不希望我的用户丢失他们的数据。

Is there a way I can tell ORMlite to start managing the already made database? Or is there a standard practice to read all of my data from the old database and write it to a new one?

有没有办法告诉ORMlite开始管理已经建立的数据库?或者是否有标准做法从旧数据库中读取所有数据并将其写入新数据库?

1 个解决方案

#1


0  

Well after doing a fair amount of due diligence I realized how simple of a task this is. Ormlite actually sits on top of the built-in SQLite. No code is needed to move to Ormlite. I simple reference my database name within my Ormlite Helper Class. My code is below. I hope this helps someone else in the future.

在做了相当多的尽职调查后,我意识到这是一项多么简单的任务。 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);
    }
}

#1


0  

Well after doing a fair amount of due diligence I realized how simple of a task this is. Ormlite actually sits on top of the built-in SQLite. No code is needed to move to Ormlite. I simple reference my database name within my Ormlite Helper Class. My code is below. I hope this helps someone else in the future.

在做了相当多的尽职调查后,我意识到这是一项多么简单的任务。 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);
    }
}