关于ormlite-android用法详解
阅读原文时间:2021年04月20日阅读:1

首先说明一下,本人已经使用ormlite-android做过两个大型的项目开发,很久以来就想对此数据库做一些总结,正好今天有空就写出来:

1. 首先去官网http://ormlite.com/看官方说明,也可以去http://ormlite.com/releases/下载两个包:一个是ormlite-core-4.24.jar,另一个是ormlite-android-4.24.jar

2. 下载完2个jar包后导入项目中,在此不多说,大家都懂的

3.前奏工作完成,下面就是怎么去搭建框架使用了,首先说明本人做过一段时间的web开发,所以喜欢用mvc模式,下面将通过代码来说明:

   1).建立自己的DatabaseHelper类

public class DatabaseHelper extends OrmLiteSqliteOpenHelper {

    // name of the database file for your application -- change to something
    // appropriate for your app
    private static final String DATABASE_NAME = "CHENGYIJI.db";

    // any time you make changes to your database objects, you may have to
    // increase the database version
    private static final int DATABASE_VERSION = 1;

    // the DAO object we use to access the SimpleData table

    //数据库默认路径SDCard

    private static String DATABASE_PATH = Environment.getExternalStorageDirectory()
            + "/CHENGYIJI.db";

    private Context mContext;



    //数据库配置文件默认路径SDCard

    private static String DATABASE_PATH_JOURN = Environment.getExternalStorageDirectory()
            + "/CHEYIJI.db-journal";


    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        mContext = context;
        initDtaBasePath();
        try {

            File f = new File(DATABASE_PATH);
            if (!f.exists()) {
                SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(DATABASE_PATH, null);
                onCreate(db);
                db.close();
            }
        } catch (Exception e) {
        }
    }



     //如果没有SDCard 默认存储在项目文件目录下

    private void initDtaBasePath() {
        if (!Utils.ExistSDCard()) {
            DATABASE_PATH = mContext.getFilesDir().getAbsolutePath() + "/CHENGYIJI.db";
            DATABASE_PATH_JOURN = mContext.getFilesDir().getAbsolutePath() + "/CHEYIJI.db-journal";
        }
    }

    @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);
    }

    /**
     * This is called when the database is first created. Usually you should
     * call createTable statements here to create the tables that will store
     * your data.
     */
    @Override
    public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
        LogTool.i(DatabaseHelper.class.getName(), "onCreate");
        try {
            TableUtils.createTable(connectionSource, UserInfo.class);
        } catch (java.sql.SQLException e) {
            LogTool.e(DatabaseHelper.class.getName(), "Can't create database", e);
            throw new RuntimeException(e);
        }

    }

    /**
     * This is called when your application is upgraded and it has a higher
     * version number. This allows you to adjust the various data to match the
     * new version number.
     */
    @Override
    public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion,
            int newVersion) {
        LogTool.i(DatabaseHelper.class.getName(), "onUpgrade");
        try {
            TableUtils.dropTable(connectionSource, UserInfo.class, true);
            onCreate(db, connectionSource);
        } catch (java.sql.SQLException e) {
            LogTool.e(DatabaseHelper.class.getName(), "Can't drop databases", e);
            throw new RuntimeException(e);
        }
    }


    public void deleteDB() {
        if (mContext != null) {
            File f = mContext.getDatabasePath(DATABASE_NAME);
            if (f.exists()) {
                // mContext.deleteDatabase(DATABASE_NAME);
                LogTool.e("DB", "---delete SDCard DB---");
                f.delete();
            } else {
                LogTool.e("DB", "---delete App DB---");
                mContext.deleteDatabase(DATABASE_NAME);
            }

            File file = mContext.getDatabasePath(DATABASE_PATH);
            if (file.exists()) {
                LogTool.e("DB", "---delete SDCard DB 222---");
                file.delete();
            }

            File file2 = mContext.getDatabasePath(DATABASE_PATH_JOURN);
            if (file2.exists()) {
                LogTool.e("DB", "---delete SDCard DB 333---");
                file2.delete();
            }
        }
    }

    /**
     * Close the database connections and clear any cached DAOs.
     */
    @Override
    public void close() {
        super.close();
    }
}

2)创建自己的数据库操作Dao层】