Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////// sqlitehelper example
- public static class MyDbOpenHelper extends SQLiteOpenHelper {
- MyDbOpenHelper(Context context) {
- //
- super(context,DbSchema.DATABASE_NAME, null, DbSchema.DATABASE_VERSION);
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- resetDb(db);
- }
- @Override
- public void onOpen(SQLiteDatabase db) {
- //open db
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- //upgrade db
- SharedPreferences.Editor editor = pref.edit();
- editor.putBoolean("dbdirty", true);
- editor.commit();
- if(writeCSVFile("dashboard_settings_backup.csv")) {
- resetDb(db);
- editor = pref.edit();
- editor.putBoolean("dbdirty", false);
- editor.commit();
- } else {
- //error, couldn't write settings
- }
- if(oldVersion<24) {
- db.execSQL(DbSchema.ScheduleSchema.DROP_TABLE);
- db.execSQL(DbSchema.ScheduleSchema.CREATE_TABLE);
- }
- }
- public void resetDb(SQLiteDatabase db) {
- db.execSQL(DbSchema.InboundSchema.DROP_TABLE);
- db.execSQL(DbSchema.OutboundSchema.DROP_TABLE);
- db.execSQL(DbSchema.CheckbookSchema.DROP_TABLE);
- db.execSQL(DbSchema.OnholdSchema.DROP_TABLE);
- db.execSQL(DbSchema.ReconciliationSchema.DROP_TABLE);
- db.execSQL(DbSchema.OnyardSchema.DROP_TABLE);
- db.execSQL(DbSchema.ScheduleSchema.DROP_TABLE);
- db.execSQL(DbSchema.InboundSchema.CREATE_TABLE);
- db.execSQL(DbSchema.OutboundSchema.CREATE_TABLE);
- db.execSQL(DbSchema.CheckbookSchema.CREATE_TABLE);
- db.execSQL(DbSchema.OnholdSchema.CREATE_TABLE);
- db.execSQL(DbSchema.ReconciliationSchema.CREATE_TABLE);
- db.execSQL(DbSchema.OnyardSchema.CREATE_TABLE);
- db.execSQL(DbSchema.ScheduleSchema.CREATE_TABLE);
- Cursor c = null;
- try {
- c = db.query(DbSchema.SiteSchema.TABLE_NAME, null, null, null, null, null, null, null);
- if(c==null || c.getCount()<1) {
- db.execSQL(DbSchema.SiteSchema.DROP_TABLE);
- db.execSQL(DbSchema.SiteSchema.CREATE_TABLE);
- } else {
- c.moveToFirst();
- List<ContentValues> values = new ArrayList<ContentValues>();
- do {
- try {
- String label = c.getString(c.getColumnIndex(DbSchema.SiteSchema.COLUMN_LABEL));
- String ip = c.getString(c.getColumnIndex(DbSchema.SiteSchema.COLUMN_IP));
- String port = c.getString(c.getColumnIndex(DbSchema.SiteSchema.COLUMN_PORT));
- ContentValues cv = new ContentValues();
- cv.put(DbSchema.SiteSchema.COLUMN_LABEL, label);
- cv.put(DbSchema.SiteSchema.COLUMN_IP, ip);
- cv.put(DbSchema.SiteSchema.COLUMN_PORT, port);
- values.add(cv);
- } catch(Exception e) {
- //couldn't get some stuff
- }
- } while(c.moveToNext());
- //now remove the old table
- db.execSQL(DbSchema.SiteSchema.DROP_TABLE);
- db.execSQL(DbSchema.SiteSchema.CREATE_TABLE);
- //re-enter old data
- for(int i=0; i<values.size(); i++) {
- try {
- db.insert(DbSchema.SiteSchema.TABLE_NAME, null, values.get(i));
- } catch(Exception e) {
- //
- }
- }
- }
- } catch(Exception e) {
- if(c==null || c.getCount()<1) {
- db.execSQL(DbSchema.SiteSchema.DROP_TABLE);
- db.execSQL(DbSchema.SiteSchema.CREATE_TABLE);
- }
- }
- }
- }
- /////////// database structure (DbSchema.java)
- package com.conceptualsystems.dashboard;
- import android.provider.BaseColumns;
- public final class DbSchema{
- public static final String DATABASE_NAME = "smsdashboard";
- public static final int DATABASE_VERSION = 24;
- public static final String SORT_ASC = " ASC";
- public static final String SORT_DESC = " DESC";
- public static final String[] ORDERS = {SORT_ASC,SORT_DESC};
- public static final int OFF = 0;
- public static final int ON = 1;
- public static final class SiteSchema implements BaseColumns {
- public static final String TABLE_NAME = "site";
- public static final String COLUMN_LABEL = "label";
- public static final String COLUMN_IP = "ip";
- public static final String COLUMN_PORT = "port";
- public static final String COLUMN_ACTIVATION_CODE = "code";
- public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_LABEL + " TEXT NOT NULL," + COLUMN_IP + " TEXT NOT NULL," + COLUMN_ACTIVATION_CODE + " TEXT," + COLUMN_PORT + " TEXT NOT NULL);";
- public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
- }
- public static final class ScheduleSchema implements BaseColumns {
- public static final String TABLE_NAME = "schedule";
- public static final String COLUMN_SITE_ID = "site_id";
- public static final String COLUMN_MONDAY = "monday";
- public static final String COLUMN_TUESDAY = "tuesday";
- public static final String COLUMN_WEDNESDAY = "wednesday";
- public static final String COLUMN_THURSDAY = "thursday";
- public static final String COLUMN_FRIDAY = "friday";
- public static final String COLUMN_SATURDAY = "saturday";
- public static final String COLUMN_SUNDAY = "sunday";
- public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_SITE_ID + " TEXT NOT NULL," + COLUMN_MONDAY + " TEXT NOT NULL," + COLUMN_TUESDAY + " TEXT NOT NULL," + COLUMN_WEDNESDAY + " TEXT NOT NULL," + COLUMN_THURSDAY + " TEXT NOT NULL," + COLUMN_FRIDAY + " TEXT NOT NULL," + COLUMN_SATURDAY + " TEXT NOT NULL," + COLUMN_SUNDAY + " TEXT NOT NULL);";
- public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
- }
- /*
- public static final class InboundSchema implements BaseColumns {
- public static final String TABLE_NAME = "inbound";
- public static final String COLUMN_NAME = "ProductName";
- public static final String COLUMN_COUNT = "Count";
- public static final String COLUMN_TOTAL = "Total";
- public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_TOTAL + " CURRENCY NOT NULL," + COLUMN_COUNT + " LONG NOT NULL," + COLUMN_NAME + " TEXT NOT NULL);";
- public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
- }
- */
- public static final class InboundSchema implements BaseColumns {
- public static final String TABLE_NAME = "inbound";
- public static final String COLUMN_NAME = "ProductName";
- public static final String COLUMN_UOM = "UOM";
- public static final String COLUMN_TOTAL = "Total";
- public static final String COLUMN_UNITS = "Units";
- public static final String COLUMN_COG = "COG";
- public static final String COLUMN_PROJECTION_UNITS = "pUnits";
- public static final String COLUMN_PROJECTION_TOTAL = "pTotal";
- public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_TOTAL + " CURRENCY NOT NULL," + COLUMN_COG + " CURRENCY NOT NULL," + COLUMN_UNITS + " FLOAT NOT NULL,"+ COLUMN_PROJECTION_UNITS + " FLOAT,"+ COLUMN_PROJECTION_TOTAL + " CURRENCY," + COLUMN_UOM + " TEXT NOT NULL," + COLUMN_NAME + " TEXT NOT NULL);";
- public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
- }
- public static final class OutboundSchema implements BaseColumns {
- public static final String TABLE_NAME = "outbound";
- public static final String COLUMN_NAME = "ProductName";
- public static final String COLUMN_UOM = "UOM";
- public static final String COLUMN_TOTAL = "Total";
- public static final String COLUMN_UNITS = "Units";
- public static final String COLUMN_COG = "COG";
- public static final String COLUMN_PROJECTION_UNITS = "pUnits";
- public static final String COLUMN_PROJECTION_TOTAL = "pTotal";
- public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_TOTAL + " CURRENCY NOT NULL," + COLUMN_COG + " CURRENCY NOT NULL," + COLUMN_UNITS + " FLOAT NOT NULL,"+ COLUMN_PROJECTION_UNITS + " FLOAT,"+ COLUMN_PROJECTION_TOTAL + " CURRENCY," + COLUMN_UOM + " TEXT NOT NULL," + COLUMN_NAME + " TEXT NOT NULL);";
- public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
- }
- public static final class CheckbookSchema implements BaseColumns {
- public static final String TABLE_NAME = "checkbook";
- public static final String COLUMN_NAME = "CheckPayToOrder";
- public static final String COLUMN_COUNT = "Count";
- public static final String COLUMN_TOTAL = "Total";
- public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_TOTAL + " CURRENCY NOT NULL," + COLUMN_COUNT + " LONG NOT NULL," + COLUMN_NAME + " TEXT NOT NULL);";
- public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
- }
- public static final class OnholdSchema implements BaseColumns {
- public static final String TABLE_NAME = "onhold";
- public static final String COLUMN_NAME = "CustomerName";
- public static final String COLUMN_UOM = "UOM";
- public static final String COLUMN_TOTAL = "Total";
- public static final String COLUMN_UNITS = "Units";
- public static final String COLUMN_COG = "COG";
- public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_TOTAL + " CURRENCY NOT NULL," + COLUMN_COG + " CURRENCY NOT NULL," + COLUMN_UNITS + " FLOAT NOT NULL," + COLUMN_UOM + " TEXT NOT NULL," + COLUMN_NAME + " TEXT NOT NULL);";
- public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
- }
- public static final class ReconciliationSchema implements BaseColumns {
- public static final String TABLE_NAME = "reconciliation";
- public static final String COLUMN_NAME = "CustomerName";
- public static final String COLUMN_UOM = "UOM";
- public static final String COLUMN_TOTAL = "Total";
- public static final String COLUMN_UNITS = "Units";
- public static final String COLUMN_COG = "COG";
- public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_TOTAL + " CURRENCY NOT NULL," + COLUMN_COG + " CURRENCY NOT NULL," + COLUMN_UNITS + " FLOAT NOT NULL," + COLUMN_UOM + " TEXT NOT NULL," + COLUMN_NAME + " TEXT NOT NULL);";
- public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
- }
- public static final class OnyardSchema implements BaseColumns {
- public static final String TABLE_NAME = "onyard";
- public static final String COLUMN_NAME = "CustomerName";
- public static final String COLUMN_UOM = "UOM";
- public static final String COLUMN_TOTAL = "Total";
- public static final String COLUMN_UNITS = "Units";
- public static final String COLUMN_COG = "COG";
- public static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + _ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_TOTAL + " CURRENCY NOT NULL," + COLUMN_COG + " CURRENCY NOT NULL," + COLUMN_UNITS + " FLOAT NOT NULL," + COLUMN_UOM + " TEXT NOT NULL," + COLUMN_NAME + " TEXT NOT NULL);";
- public static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement