Created
November 23, 2015 22:53
-
-
Save jy2k/3c89edd058670bac22ff to your computer and use it in GitHub Desktop.
The NOT singleton version of the DatabaseHelper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.database.sqlite.SQLiteDatabase; | |
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper; | |
import com.j256.ormlite.dao.Dao; | |
import com.j256.ormlite.support.ConnectionSource; | |
import com.j256.ormlite.table.TableUtils; | |
import java.sql.SQLException; | |
/** | |
* Database helper class used to manage the creation and upgrading of your database. This class also usually provides | |
* the DAOs used by the other classes. | |
*/ | |
public class DatabaseHelper | |
extends OrmLiteSqliteOpenHelper { | |
/** | |
* Returns the Database Access Object (DAO) for our Citizen class. | |
*/ | |
public Dao<SoldierDBItem, Integer> getSoldierDao() throws | |
SQLException { | |
return getDao(SoldierDBItem.class); | |
} | |
// name of the database file for your application -- change to something appropriate for your app | |
private static final String DATABASE_NAME = "Soldiers.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 citizens table | |
private Dao<SoldierDBItem, Integer> mSoldierDao = null; | |
public DatabaseHelper(Context context) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
/** | |
* 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) { | |
try { | |
TableUtils.createTable(connectionSource, SoldierDBItem.class); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* 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) { | |
try { | |
TableUtils.dropTable(connectionSource, SoldierDBItem.class, true); | |
// after we drop the old databases, we create the new ones | |
onCreate(db, connectionSource); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
/** | |
* Close the database connections and clear any cached DAOs. | |
*/ | |
@Override | |
public void close() { | |
super.close(); | |
mSoldierDao = null; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment