The ORM is a very useful invention, which made our lives easier in Android cent of our development. Let's take the Database Helper implementation.
The class DatabaseHelper
The purpose of the database DatabaseHelper class and the DAO's creation and destruction. The ORM application on Android - Part Foreword written by the time of the DatabaseHelper, so now comes the selfcommented source. :)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 At 25 26 27 28 29 30 31, 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71, 72 73 74 To 75 76 77 78 79 To 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94, 95 96 97 98, 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 com.example.db.utils package; import java.sql.SQLException; android.content.Context import; android.database.sqlite.SQLiteDatabase import; android.util.Log import; com.example.db.dao.CourseDao import; com.example.db.dao.CourseTakenDao import; com.example.db.dao.StudentDao import; com.example.db.entity.Course import; com.example.db.entity.CourseTaken import; com.example.db.entity.Student import; com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper import; com.j256.ormlite.support.ConnectionSource import; com.j256.ormlite.table.TableUtils import; / ** * Creates Database helper and drops the database tables. It Manages the * Creation and deletion of the DAOS too. * * @ Author DEE * / DatabaseHelper {public class extends OrmLiteSqliteOpenHelper / ** * The name of the database. * / private static final String database_name = "studentORM.db"; / ** * The version of the database. It's needed to update every new release. * / private static final int Database_Version = 1; / ** * Student DAO. * / private StudentDao studentDao = null; / ** * Course DAO. * / private CourseDao courseDao = null; / ** * CourseTaken DAO. * / private CourseTakenDao courseTakenDao = null; / ** * It's required by OrmLiteSqliteOpenHelper. * / public DatabaseHelper (Context context) { super (context, database_name, null, Database_Version); } / ** * This is Called When the database is first created. * / @ Override public void onCreate (SQLiteDatabase db, ConnectionSource connectionSource) { try { / / Create table Student . TableUtils createTableIfNotExists (connectionSource, Student class.) / / Create table Course . TableUtils createTableIfNotExists (connectionSource, Course class.) / / Create table CourseTaken . TableUtils createTableIfNotExists (connectionSource, CourseTaken class.) . Log e ("DEE" "Database created."); } Catch (SQLException se) { . These log ("DEE", "Can not create database.", Se); throw new RuntimeException (se); } } / ** * This is Called When your application is upgraded and it has a higher * Version number. * / @ Override public void onUpgrade (SQLiteDatabase db, ConnectionSource connectionSource, oldVersion int, int newVersion) { try { / / Drop all the tables. . TableUtils dropTable (connectionSource, Student class, true.) . TableUtils dropTable (connectionSource, Course class, true.) . TableUtils dropTable (connectionSource, CourseTaken 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 not drop databases", e.) throw new RuntimeException (e); } } / ** * Returns the Database Access Object (DAO) for our Student class. It will * Create it or just give the cached value. * / public StudentDao getStudentDao () throws SQLException { return (studentDao == null? studentDao = getDao (Student. class) : StudentDao); } / ** * Returns the Database Access Object (DAO) for our Course class. It will * Create it or just give the cached value. * / public CourseDao getCourseDao () throws SQLException { return (courseDao == null? courseDao = getDao (Course. class) : CourseDao); } / ** * Returns the Database Access Object (DAO) for our Course class. It will * Create it or just give the cached value. * / public CourseTakenDao getCourseTakenDao () throws SQLException { return (courseTakenDao == null? courseTakenDao = getDao (CourseTaken. class) : CourseTakenDao); } / ** * Close the database connections and clear any cached Daos. * / @ Override public void close () { . super close (); / / Clear the DAOS studentDao = null; courseDao = null; courseTakenDao = null; } }
