Android Open Source - AndroidSqliteMultichoiceExample Questions Open Helper






From Project

Back to project page AndroidSqliteMultichoiceExample.

License

The source code is released under:

Apache License

If you think the Android project AndroidSqliteMultichoiceExample listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
 * Copyright 2014 Yehezkel (Zack) Yovel/*from   w  ww.j a  va2 s  .  c  o  m*/
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package zack.examples.questiondb.database;

import java.io.IOException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

public class QuestionsOpenHelper extends SQLiteOpenHelper {

  public static final String DB_CONTENT_URL = "https://raw.githubusercontent.com/ZackYovel/AndroidSqliteMultichoiceExample/master/sql/contents.sql";
  public static final String DB_NAME = "Questions.db";
  public static final int DB_VERSION = 1;
  private static final String TAG = "QuestionsOpenHelper";

  public QuestionsOpenHelper(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase db) {
    // executing the schema - DDL (data definition language)
    db.execSQL(DBContract.QuestionsTable.SCHEMA);
    db.execSQL(DBContract.AnswersTable.SCHEMA);

    try {
      OkHttpClient client = new OkHttpClient();

      Request request = new Request.Builder().url(DB_CONTENT_URL).build();

      Response response = client.newCall(request).execute();
      String[] sql = response.body().string().split("===");

      // executing data insertion - DML (data manipulation language)
      for (String statement : sql) {
        db.execSQL(statement.trim());
      }

    } catch (IOException e) {
      e.printStackTrace();
      Log.d(TAG, e.getMessage());
    }

  }

  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // If the schema changes this is where you read data from the old table
    // defined by the old schema and adapt it to the new schema. Then
    // you re-create the table using the new schema and insert all data back
    // in.

    // in many tutorials they re-create the database here even if there is
    // nothing to change:
    db.execSQL(DBContract.QuestionsTable.DELETE_SCHEMA);
    db.execSQL(DBContract.AnswersTable.DELETE_SCHEMA);
    onCreate(db);
  }

}




Java Source Code List

zack.examples.questiondb.MainActivity.java
zack.examples.questiondb.QuestionsActivity.java
zack.examples.questiondb.database.DBContract.java
zack.examples.questiondb.database.DataAccessObject.java
zack.examples.questiondb.database.QuestionsOpenHelper.java
zack.examples.qustiondb.entities.Question.java