Last active
August 17, 2017 23:13
-
-
Save victorman/fcfeb21315b5e8f1431d to your computer and use it in GitHub Desktop.
Templates for SQLite database contract and helper classes. To add these go to File > New > Edit File Templates... . In the Templates tab click the + icon. Give an appropriate name and paste the code in below. To use just go File > New > Your Template.
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
package ${PACKAGE_NAME}; | |
#parse("File Header.java") | |
public class ${NAME} { | |
public ${NAME}() {} | |
public static class ${ENTRY_CLASS} implements BaseColumns { | |
public static final String TABLE_NAME = "${TABLE_NAME}"; | |
public static final String COLUMN_NAME_CREATED = "date_created"; | |
} | |
} |
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
#if (${PACKAGE_NAME} && ${PACKAGE_NAME} != "")package ${PACKAGE_NAME};#end | |
import ${PACKAGE_NAME}.${CONTRACT_CLASS}.${ENTRY_CLASS}; | |
#parse("File Header.java") | |
public class ${NAME} extends SQLiteOpenHelper { | |
/* | |
* Database version and name. | |
*/ | |
public static final int DATABASE_VERSION = 1; | |
public static final String DATABASE_NAME = "${DATABASE_NAME}"; | |
/* | |
* quick database type constants and a comma. | |
* pay attention to where spaces are inside your strings | |
* when using constants. you don't want to end up with | |
* something like " INTEGERNOT NULL" for a type. | |
*/ | |
private static final String TEXT_TYPE = " TEXT"; | |
private static final String INT_TYPE = " INTEGER"; | |
private static final String REAL_TYPE = " REAL"; | |
private static final String C = ","; | |
private static final String SQL_CREATE_ENTRIES = | |
"CREATE TABLE " + ${ENTRY_CLASS}.TABLE_NAME + " (" + | |
${ENTRY_CLASS}._ID + INT_TYPE + " PRIMARY KEY AUTOINCREMENT" + C + | |
${ENTRY_CLASS}.COLUMN_NAME_CREATED + TEXT_TYPE + C + | |
/* | |
* TODO Define columns here | |
* column definitions are comma seperated | |
* COLUMN_NAME TYPE (example:) | |
* name TEXT, | |
* age INTEGER | |
*/ | |
" )"; | |
private static final String SQL_DELETE_ENTRIES = | |
"DROP TABLE IF EXISTS " + ${ENTRY_CLASS}.TABLE_NAME; | |
public ${NAME}(Context context) { | |
super(context, DATABASE_NAME, null, DATABASE_VERSION); | |
} | |
@Override | |
public void onCreate(SQLiteDatabase db) { | |
db.execSQL(SQL_CREATE_ENTRIES); | |
} | |
@Override | |
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { | |
db.execSQL(SQL_DELETE_ENTRIES); | |
onCreate(db); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment