Пример #1
0
 // Must be called during app initialization i.e. splash activity
 public void createDatabase() {
   boolean dbExist = checkDataBase();
   if (dbExist) {
     this.close();
   } else {
     // By calling this method and empty database will be created into the default system path
     // of your application so we are gonna be able to overwrite that database with our database.
     this.getReadableDatabase();
     try {
       copyDataBase();
       AppInstance.setAppInitState(true);
     } catch (IOException e) {
       e.printStackTrace();
       Log.e(TAG, "onCreate: failed to copy database : " + e.getMessage());
     }
   }
 }
Пример #2
0
  /** Change poi table columns. Remove the position column and add latitude and longitude columns */
  public void updateDatabaseData(SQLiteDatabase database) {
    Log.d(TAG, "updateDatabaseData() called");
    // 1. backup the user created poi's
    List<POI> userCreatedPoiList = new ArrayList<>();
    String getUserPoiSql =
        "SELECT * FROM "
            + PoiModel.TABLE_NAME
            + " poi "
            + " INNER JOIN "
            + SQLiteDbAdapter.TABLE_CATEGORY
            + " c ON poi."
            + PoiModel.CATEGORY
            + " = c."
            + SQLiteDbAdapter.CATEGORY_ID
            + " where poi.p_type = 1";

    Cursor cursor = null;
    cursor = database.rawQuery(getUserPoiSql, null);
    if (cursor.moveToFirst()) {
      do {

        userCreatedPoiList.add(
            new POI(
                cursor.getInt(cursor.getColumnIndex(PoiModel.ID)),
                cursor.getString(cursor.getColumnIndex(PoiModel.NAME)),
                cursor.getString(cursor.getColumnIndex(PoiModel.ADDRESS)),
                cursor.getString(cursor.getColumnIndex(PoiModel.EMAIL)),
                cursor.getString(cursor.getColumnIndex(PoiModel.PHONE)),
                cursor.getString(cursor.getColumnIndex(PoiModel.WEB)),
                Functions.convert(cursor.getString(cursor.getColumnIndex("p_position"))),
                cursor.getInt(cursor.getColumnIndex(PoiModel.TYPE)),
                cursor.getInt(cursor.getColumnIndex(PoiModel.CATEGORY_CLASS)),
                new Category(
                    cursor.getInt(cursor.getColumnIndex(SQLiteDbAdapter.CATEGORY_ID)),
                    cursor.getString(cursor.getColumnIndex(SQLiteDbAdapter.CATEGORY_LABEL)))));
      } while (cursor.moveToNext());
    }
    cursor.close();

    // 2. Backup the bookmarks table
    List<Bookmark> bookmarks = new ArrayList<>();

    String SQL =
        "SELECT * FROM "
            + SQLiteDbAdapter.TABLE_BOOKMARK
            + " b "
            + "INNER JOIN "
            + PoiModel.TABLE_NAME
            + " p ON b."
            + SQLiteDbAdapter.BOOKMARK_POI
            + " = p."
            + PoiModel.ID
            + " "
            + "INNER JOIN "
            + SQLiteDbAdapter.TABLE_CATEGORY
            + " c ON p."
            + PoiModel.CATEGORY
            + " = c."
            + SQLiteDbAdapter.CATEGORY_ID;

    cursor = database.rawQuery(SQL, null);

    if (cursor.moveToFirst()) {
      do {
        bookmarks.add(
            new Bookmark(
                cursor.getInt(cursor.getColumnIndexOrThrow(SQLiteDbAdapter.BOOKMARK_ID)),
                new POI(
                    cursor.getInt(cursor.getColumnIndex(PoiModel.ID)),
                    cursor.getString(cursor.getColumnIndex(PoiModel.NAME)),
                    cursor.getString(cursor.getColumnIndex(PoiModel.ADDRESS)),
                    cursor.getString(cursor.getColumnIndex(PoiModel.EMAIL)),
                    cursor.getString(cursor.getColumnIndex(PoiModel.PHONE)),
                    cursor.getString(cursor.getColumnIndex(PoiModel.WEB)),
                    Functions.convert(cursor.getString(cursor.getColumnIndex("p_position"))),
                    cursor.getInt(cursor.getColumnIndex(PoiModel.TYPE)),
                    cursor.getInt(cursor.getColumnIndex(PoiModel.CATEGORY_CLASS)),
                    new Category(
                        cursor.getInt(cursor.getColumnIndex(SQLiteDbAdapter.CATEGORY_ID)),
                        cursor.getString(cursor.getColumnIndex(SQLiteDbAdapter.CATEGORY_LABEL)))),
                cursor.getString(cursor.getColumnIndex(SQLiteDbAdapter.BOOKMARK_DATE))));

      } while (cursor.moveToNext());
    }
    cursor.close();

    Meta mapMeta = null;

    String sql =
        "SELECT * FROM "
            + SQLiteDbAdapter.TABLE_META
            + " WHERE "
            + SQLiteDbAdapter.META_TYPE
            + "='M'";
    cursor = database.rawQuery(sql, null);
    if (cursor.moveToFirst()) {
      mapMeta =
          new Meta(
              cursor.getString(cursor.getColumnIndex(SQLiteDbAdapter.META_TYPE)),
              cursor.getInt(cursor.getColumnIndex(SQLiteDbAdapter.META_VERSION)));
    } else {
      mapMeta = new Meta("M", 1);
    }

    Log.d(TAG, "updateDatabaseData: Backup done ");
    // 3. now truncate the database
    truncateDatabase(database);
    Log.d(TAG, "updateDatabaseData: Truncate done");
    // 4. copy the database
    try {
      copyDataBase();
    } catch (IOException e) {
      Log.e(TAG, "updateDatabaseData: --> " + e.getMessage());
    }

    // 5. now re-enter the user's poi
    for (POI poi : userCreatedPoiList) {
      ContentValues values = new ContentValues();
      values.put(PoiModel.ID, poi.getId());
      values.put(PoiModel.NAME, poi.getName());
      values.put(PoiModel.ADDRESS, poi.getAddress());
      values.put(PoiModel.EMAIL, poi.getEmail());
      values.put(PoiModel.PHONE, poi.getPhone());
      values.put(PoiModel.WEB, poi.getWebsite());
      values.put(PoiModel.LATITUDE, poi.getLocation().getLatitude());
      values.put(PoiModel.LONGITUDE, poi.getLocation().getLongitude());
      values.put(PoiModel.TYPE, poi.getPoiType());
      values.put(PoiModel.CATEGORY_CLASS, poi.getCategoryClass());
      values.put(PoiModel.CATEGORY, poi.getCategory().getId());
      database.insert(PoiModel.TABLE_NAME, null, values);
    }

    // 6. now re-enter the bookmarks database
    for (Bookmark bookmark : bookmarks) {
      ContentValues values = new ContentValues();
      values.put(SQLiteDbAdapter.BOOKMARK_POI, bookmark.getPoi().getId());
      database.insert(SQLiteDbAdapter.TABLE_BOOKMARK, null, values);
    }

    ContentValues contentValues = new ContentValues();
    contentValues.put(SQLiteDbAdapter.META_TYPE, mapMeta.getMetaType());
    contentValues.put(SQLiteDbAdapter.META_VERSION, mapMeta.getMetaVersion());
    database.insert(SQLiteDbAdapter.TABLE_META, null, contentValues);

    // re-initialize the app state
    AppInstance.setAppInitState(true);
  }