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