Last active
November 27, 2017 13:36
-
-
Save masztalski/d318e71e0932007868508fc160b5447d to your computer and use it in GitHub Desktop.
This file contains 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
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 = "DB_NAME.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 | |
public DatabaseHelper(Context context) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
/* | |
What to do when your database needs to be created. Usually this entails creating the tables and loading any initial data. | |
*/ | |
@Override | |
public void onCreate(SQLiteDatabase database, ConnectionSource connectionSource) { | |
try { | |
TableUtils.createTable(connectionSource, User.class); | |
} catch (java.sql.SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
/* | |
What to do when your database needs to be updated. This could mean careful migration of old data to new data. | |
Maybe adding or deleting database columns, etc.. | |
*/ | |
@Override | |
public void onUpgrade(SQLiteDatabase database, ConnectionSource connectionSource, | |
int oldVersion, int newVersion) { | |
(oldVersion != newVersion){ | |
try { | |
TableUtils.dropTable(connectionSource, User.class, true); | |
} catch (java.sql.SQLException e) { | |
e.printStackTrace(); | |
} | |
onCreate(database, connectionSource); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment