Android Open Source - FlyingDB Database Helper






From Project

Back to project page FlyingDB.

License

The source code is released under:

MIT License

If you think the Android project FlyingDB 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

//**********************************************************
//************** AUTOGENERATED with FLYINGDB ***************
//**********************************************************
//from  w ww. ja  v  a 2s.  c  o  m
package com.flyingboba.flyingdb;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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

public class DatabaseHelper extends SQLiteOpenHelper {

  private static final String DATABASE_NAME = "test.sqlite";
  private static final int DATABASE_VERSION = 1;
  private final Context myContext;
  
  //
  // DATABASE_PATH is only used when copying an existing database from the packaged app to storage space on the phone.
  // Path should be similar to "/data/data/com.flyingboba.flyingdbexample/databases/" 
  // except with your package name instead of com.flyingboba.flyingdbexample. The package name can be found in the 
  // AndroidManifest.xml
  private static final String DATABASE_PATH = "/data/data/com.flyingboba.flyingdbexample/databases/";

  
  public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.myContext = context;
  }

  //
  // createDatabase, checkDatabase, copyDatabase are all methods used to facilitate the copying of an existing database 
  // from the .apk to storage space on the phone.
  //
  public void createDatabase() throws IOException {
    
      boolean dbExist = checkDatabase();
       
      if(dbExist){
        Log.i("createDatabase", "database already exists");
      }else{
 
        // Get readable database creates the database, directories necessary
             this.getReadableDatabase();
             this.close();
           
          try {
 
          copyDatabase();
 
        } catch (IOException e) {
 
            throw new Error("Error copying database");
 
          }
      }
    
  }
  
  
    private boolean checkDatabase() {
       
      SQLiteDatabase checkDB = null;
 
      try{
        String myPath = DATABASE_PATH + DATABASE_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
        // Do not call this.getReadableDatabase() bc it will not throw an exception, but instead create an empty database
        checkDB.close();
        return true;
      }catch(SQLiteException e){ 
        Log.i("Sqlite exception",e.getStackTrace().toString());
      }
 
      return false;
    }
  
  
    private void copyDatabase() throws IOException {
       
      String[] fileNames = myContext.getAssets().list("");
      for(String name:fileNames){
           Log.i("Asset item", name + "\n");    
      }
      
      InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
 
      String outFileName = DATABASE_PATH + DATABASE_NAME;
        
      OutputStream myOutput = new FileOutputStream(outFileName);
       
      byte[] buffer = new byte[1024];
      int length;
      while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
      }
 
      myOutput.flush();
      myOutput.close();
      
      myInput.close();
 
    }
      
    //  
    // If creating database with code, do it here.
    //
    @Override
        public void onCreate(SQLiteDatabase db) {
    }

    @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    }
  
  
  
}




Java Source Code List

com.flyingboba.flyingdb.DatabaseAdapter.java
com.flyingboba.flyingdb.DatabaseHelper.java
com.flyingboba.flyingdb.PrefixEvent.java
com.flyingboba.flyingdb.PrefixLocation.java
com.flyingboba.flyingdb.PrefixPerson.java
com.flyingboba.flyingdbexample.MainActivity.java