title: Import Sqlite Database into device programmatically
body: <p>I have manage to export SQLite database to sdcard now I will appreciate if someone help to Import Sqlite database into device?</p>
  
  <p>i'm newbi in android I have tried a lot without successful. </p>
  
  <p>Edit….</p>
  
  <pre><code>public class MainActivity extends Activity implements OnClickListener {
  
  private static final String SAMPLE_DB_NAME = "TrekBook";
  private static final String SAMPLE_TABLE_NAME = "Info";
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      findViewById(R.id.button1).setOnClickListener(this);
      findViewById(R.id.button2).setOnClickListener(this);
      findViewById(R.id.button3).setOnClickListener(this);
      findViewById(R.id.button4).setOnClickListener(this);
  
  }
  @Override
  public void onClick(View v) {
      switch(v.getId()) {
      case R.id.button1:
          deleteDB();
          break;
      case R.id.button2:
          exportDB();
          break;
      case R.id.button3:
          createDB();
          break;
      case R.id.button4:
          importDB();
          break;  
      }
  }
  
  private void deleteDB(){
      boolean result = this.deleteDatabase(SAMPLE_DB_NAME);
      if (result==true) {
           Toast.makeText(this, "DB Deleted!", Toast.LENGTH_LONG).show();
      } 
  }
  
  private void exportDB(){
      File sd = Environment.getExternalStorageDirectory();
      File data = Environment.getDataDirectory();
      FileChannel source=null;
      FileChannel destination=null;
      String currentDBPath = "/data/"+ "com.authorwjf.sqliteexport" +"/databases/"+SAMPLE_DB_NAME;
      String backupDBPath = SAMPLE_DB_NAME;
      File currentDB = new File(data, currentDBPath);
      File backupDB = new File(sd, backupDBPath);
      try {
          source = new FileInputStream(currentDB).getChannel();
          destination = new FileOutputStream(backupDB).getChannel();
          destination.transferFrom(source, 0, source.size());
          source.close();
          destination.close();
          Toast.makeText(this, "DB Exported!", Toast.LENGTH_LONG).show();
      } catch(IOException e) {
          e.printStackTrace();
      }
  }
  
  private void createDB() {
      SQLiteDatabase sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);
      sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
              SAMPLE_TABLE_NAME +
              " (LastName VARCHAR, FirstName VARCHAR," +
              " Rank VARCHAR);");
      sampleDB.execSQL("INSERT INTO " +
              SAMPLE_TABLE_NAME +
              " Values ('Kirk','James, T','Captain');");
      sampleDB.close();
      sampleDB.getPath();
      Toast.makeText(this, "DB Created @ "+sampleDB.getPath(), Toast.LENGTH_LONG).show(); 
  }
  
  private void importDB(){
      File f=new File("/data/data/com.authorwjf.sqliteexport/databases/TrekBook");
      FileInputStream fis=null;
      FileOutputStream fos=null;
  
      try
      {
        fis=new FileInputStream(f);
        fos=new FileOutputStream("/mnt/sdcard/DB/TrekBook");
        while(true)
        {
          int i=fis.read();
          if(i!=-1)
          {fos.write(i);}
          else
          {break;}
        }
        fos.flush();
        Toast.makeText(this, "DB dump OK", Toast.LENGTH_LONG).show();
      }
      catch(Exception e)
      {
        e.printStackTrace();
        Toast.makeText(this, "DB dump ERROR", Toast.LENGTH_LONG).show();
      }
      finally
      {
        try
        {
          fos.close();
          fis.close();
        }
        catch(Exception ioe)
        {}
      }
  }
  
   }
  </code></pre>
  
  <p>activity_main</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_gravity="center"
  android:layout_width="match_parent"
  android:layout_height="match_parent" &gt;
  
  &lt;TextView
      android:id="@+id/textView1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="SQLite DB to SD Demo"/&gt;
  
  &lt;Button
      android:id="@+id/button1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Delete" /&gt;
  &lt;Button
      android:id="@+id/button2"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Export" /&gt;
  &lt;Button
      android:id="@+id/button3"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Create" /&gt;
  &lt;Button
      android:id="@+id/button4"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Import" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>Thanks in advance….</p>