android的ORMlite框架研究,android端的ormlite框架

packagecom.example.dell.db;importandroid.content.Context;importandroid.util.Log;importcom.j256.ormlite.cipher.android.apptools.OrmLiteSqliteOpenHelper;importcom.j256.ormlite.dao.Dao;importcom.j256.ormlite.dao.RuntimeExceptionDao;importcom.j256.ormlite.stmt.PreparedQuery;importcom.j256.ormlite.support.ConnectionSource;importcom.j256.ormlite.table.TableUtils;importnet.sqlcipher.database.SQLiteDatabase;importjava.sql.SQLException;importjava.util.List;/*** Created by wu-pc on 2018/5/9.

* Copied from official example, and revised for my own purpose*/

public class DatabaseHelper extendsOrmLiteSqliteOpenHelper {final static private String TAG = "DatabaseHelper";//name of the database file for your application -- change to something appropriate for your app

private static final String DATABASE_NAME = "heartTrace.db";//any time you make changes to your database objects, you may have to increase the database version

private static final int DATABASE_VERSION = 2;//the DAO object we use to access the Diary table

private Dao diaryDao = null;private RuntimeExceptionDao runtimeDiaryDao = null;private Dao diaryLabelDao = null;private RuntimeExceptionDao runtimeDiaryLabelDao = null;private Dao diarybookDao = null;private RuntimeExceptionDao runtimeDiarybookDao = null;private Dao sentenceDao = null;private RuntimeExceptionDao runtimeSentenceDao = null;private Dao labelDao = null;private RuntimeExceptionDao runtimeLabelDao = null;private Dao sentenceLabelDao = null;private RuntimeExceptionDao runtimeSentenceLabelDao = null;private Dao sentencebookDao = null;private RuntimeExceptionDao runtimeSentencebookDao = null;private Dao searchHistoryDao = null;private RuntimeExceptionDao runtimeSearchHistoryDao = null;private PreparedQuerylabelForDiaryQuery;private PreparedQuerydiaryForLabelQuery;publicDatabaseHelper(Context context) {super(context, DATABASE_NAME, null, DATABASE_VERSION);

SQLiteDatabase.loadLibs(context);

}/*** 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.*/

public voidonCreate(SQLiteDatabase db, ConnectionSource connectionSource) {try{

Log.i(DatabaseHelper.class.getName(), "onCreate");

TableUtils.createTable(connectionSource, Diary.class);

TableUtils.createTable(connectionSource, Diarybook.class);

TableUtils.createTable(connectionSource, DiaryLabel.class);

TableUtils.createTable(connectionSource, Label.class);

TableUtils.createTable(connectionSource, Sentence.class);

TableUtils.createTable(connectionSource, SentenceLabel.class);

TableUtils.createTable(connectionSource, Sentencebook.class);

TableUtils.createTable(connectionSource, SearchHistory.class);

}catch(SQLException e) {

Log.e(DatabaseHelper.class.getName(), "Can't create database", e);throw newRuntimeException(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.

* Don't need it by now.*/

public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource, int oldVersion, intnewVersion) {try{

Log.i(DatabaseHelper.class.getName(), "onUpgrade");

TableUtils.dropTable(connectionSource, Diary.class, true);

TableUtils.dropTable(connectionSource, Diarybook.class, true);

TableUtils.dropTable(connectionSource, DiaryLabel.class, true);

TableUtils.dropTable(connectionSource, Label.class, true);

TableUtils.dropTable(connectionSource, Sentence.class, true);

TableUtils.dropTable(connectionSource, SentenceLabel.class, true);

TableUtils.dropTable(connectionSource, Sentencebook.class, true);

TableUtils.dropTable(connectionSource, SearchHistory.class, true);//after we drop the old databases, we create the new ones

onCreate(db, connectionSource);

}catch(SQLException e) {

Log.e(DatabaseHelper.class.getName(), "Can't drop databases", e);throw newRuntimeException(e);

}

}protectedString getPassword() {return "hello I'm password";

}/*** Returns the Database Access Object (DAO) for our Diary class. It will create it or just give the cached

* value.*/

public Dao getDiaryDao() throwsSQLException {if (diaryDao == null) {

diaryDao= getDao(Diary.class);

}returndiaryDao;

}public Dao getSentenceDao() throwsSQLException {if(sentenceDao == null) {

sentenceDao= getDao(Sentence.class);

}returnsentenceDao;

}public Dao getLabelDao() throwsSQLException {if (labelDao == null) {

labelDao= getDao(Label.class);

}returnlabelDao;

}public Dao getDiaryLabelDao() throwsSQLException {if (diaryLabelDao == null) {

diaryLabelDao= getDao(DiaryLabel.class);

}returndiaryLabelDao;

}//for the sentence

public Dao getSentenceLabelDao() throwsSQLException {if (sentenceLabelDao == null) {

sentenceLabelDao= getDao(SentenceLabel.class);

}returnsentenceLabelDao;

}public Dao getSentencebookDao() throwsSQLException {if (sentencebookDao == null) {

sentencebookDao= getDao(Sentencebook.class);

}returnsentencebookDao;

}public Dao getDiarybookDao() throwsSQLException {if (diarybookDao == null) {

diarybookDao= getDao(Diarybook.class);

}returndiarybookDao;

}public Dao getSearchHistoryDao() throwsSQLException {if (searchHistoryDao == null) {

searchHistoryDao= getDao(SearchHistory.class);

}returnsearchHistoryDao;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值