title: How to import existing SQLite database to Android Studio
body: <p>I've got an existing SQLite database that I want to work with my Android application. I have looked at all the tutorials online, which are not working for me. I am getting an error when I launch the app in the emulator that says the table I have created in the database does not exist *(android.database.sqlite.SQLiteException: no such table: adk_peaks (code 1): , while compiling: SELECT * FROM adk_peaks WHERE 1)*. </p>
  
  <p>adk_peaks is the name of the table. Right now I am only trying to get the information from the table to display in a TextView.</p>
  
  <p>Below is my extension of SQLiteOpenHelper:</p>
  
  <pre><code>package kyle.peaktracker;
  
  import android.database.SQLException;
  import android.database.sqlite.SQLiteException;
  import android.database.sqlite.SQLiteOpenHelper;
  import android.database.sqlite.SQLiteDatabase;
  import android.database.Cursor;
  import android.content.Context;
  import android.content.ContentValues;
  import android.util.Log;
  
  import java.io.File;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  
  public class MyDBHandler extends SQLiteOpenHelper {
  
      public static final int DATABASE_VERSION = 2;
      private static final String DATABASE_NAME = "peaks.db";
      //private static final String DATABASE_PATH =             "/app/src/main/assests/Database/";
      //private static final String DATABASE_PATH =     "/data/data/kyle.peaktracker/databases/";
      //private static final String DATABASE_PATH = "\\app\\src\\main\\assets\\Database\\";
      public static String DATABASE_PATH = ""; //final?
      private static final String TABLE_ADK = "adk_peaks";
      private SQLiteDatabase myDataBase;
      private final Context myContext;
  
  
      //Columns
      private static final String COLUMN_ID = "_id";
      private static final String COLUMN_NAME = "_name";
      private static final String COLUMN_HEIGHT = "_height";
      private static final String COLUMN_CLIMBED = "_climbed";
      private static final String COLUMN_DATE = "_date";
      private static final String COLUMN_LIST = "_list";
  
      public MyDBHandler(Context context){
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
          this.myContext = context;
          DATABASE_PATH = myContext.getDatabasePath(DATABASE_NAME).toString();
      }
  
      public void createDatabase() throws IOException
      {
          boolean dbExist = checkDataBase();
          if(dbExist)
          {
              Log.v("DB Exists", "db exists");
  
          }
          boolean dbExist1 = checkDataBase();
          if(!dbExist1)
          {
              this.getReadableDatabase();
              try
              {
                  this.close();
                  copyDataBase();
              }
              catch (IOException e)
              {
                  throw new Error("Error copying database");
              }
          }
      }
  
      //Check database already exist or not
      private boolean checkDataBase()
      {
          boolean checkDB = false;
          try
          {
              String myPath = DATABASE_PATH;
              File dbfile = new File(myPath);
              checkDB = dbfile.exists();
          }
          catch(SQLiteException e)
          {
          }
          return checkDB;
      }
  
  
     private void copyDataBase() throws IOException
     {
          String outFileName = DATABASE_PATH;
          OutputStream myOutput = new FileOutputStream(outFileName);
          InputStream myInput = myContext.getAssets().open(DATABASE_NAME);
          byte[] buffer = new byte[1024];
          int length;
          while ((length = myInput.read(buffer)) &gt; 0)
          {
              myOutput.write(buffer, 0, length);
          }
          myInput.close();
          myOutput.flush();
          myOutput.close();
      }
  
      public void db_delete()
      {
          File file = new File(DATABASE_PATH);
          if(file.exists())
          {
              file.delete();
              System.out.println("delete database file.");
          }
      }
  
      //Open database
      public void openDatabase() throws SQLException
      {
          String myPath = DATABASE_PATH;
          myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
      }
  
      public synchronized void closeDataBase()throws SQLException
      {
          if(myDataBase != null)
              myDataBase.close();
          super.close();
      }
  
  
      //Override onCreate and onUpgrade
      public void onCreate(SQLiteDatabase db){
          /*String query = "CREATE TABLE " + TABLE_ADK + "(" +
                  COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
                  COLUMN_NAME + " TEXT " +
                  COLUMN_HEIGHT + " INTEGER " +
                  COLUMN_CLIMBED + " INTEGER " +
                  COLUMN_DATE + " TEXT " +
                  COLUMN_LIST + " TEXT " +
                  ");";
  
          db.execSQL(query); */
      }
  
      public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)    {
          db.execSQL("DROP TABLE IF EXISTS " + TABLE_ADK);
          onCreate(db);
      }
  
      public String databaseToString(){
          String dbString = "";
          SQLiteDatabase db = getWritableDatabase();
          String query = "SELECT * FROM " + TABLE_ADK + " WHERE 1";
  
          Cursor c = db.rawQuery(query, null);
  
          c.moveToFirst();
  
          while(!c.isAfterLast()){
              if(c.getString(c.getColumnIndex("_name"))!= null){
                  dbString += c.getString(c.getColumnIndex("_name"));
                  dbString += "\n";
              }
          }
          c.close(); //Added
          db.close();
          return dbString;
      }
  }
  </code></pre>
  
  <p>This is my MainActivity:
      package kyle.peaktracker;</p>
  
  <pre><code>import android.database.SQLException;
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.widget.TextView;
  
  import java.io.IOException;
  import java.util.List;
  
  public class MainActivity extends AppCompatActivity {
  
      TextView testText;
      MyDBHandler dbHandler;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          testText = (TextView) findViewById(R.id.test_text);
          //dbHandler = new MyDBHandler(this, null, null, 1);
          dbHandler = new MyDBHandler(this);
          try {
              dbHandler.createDatabase();
          } catch (IOException ioe){
              throw new Error("Unable to create database");
          }
  
          try {
  
              dbHandler.openDatabase();
  
          }catch(SQLException sqle){
  
              throw sqle;
  
          }
  
          printDatabase();
      }
  
      public void printDatabase(){
          String dbString = dbHandler.databaseToString();
          testText.setText(dbString);
      }
  }
  </code></pre>
  
  <p>Any advice is appreciated!</p>