title: SQLiteException: table already exists (code 1)
body: <p><strong>FINAL EDIT, ISSUE "SOLVED"</strong></p>
  
  <p>So what I ended up doing as per the answer accepted, I removed the constructor that took a database version as params, but that still caused the exception. So as also suggested, I addeda <code>try{} catch{}</code> block to catch the issue, and let me tell you, it still shows throws the exception, only this time it doesn't crash the application. Luckily, despite of it seeming to have issues creating the database, it allows me to load the db content into the application, so I'm fine with that log message for now :)</p>
  
  <p>Here's the onCreate: </p>
  
  <pre><code>@Override
  public void onCreate(SQLiteDatabase db) {
      Constants.logMessage("Creating database from scratch?");
      //Create both of the tables as part of the databases
      try {
          db.execSQL(CREATE_TABLE_COUNTRIES);
          db.execSQL(CREATE_TABLE_REGIONS);
          db.execSQL(CREATE_TABLE_CITIES);
      }
      catch (Exception e) {
          //This happens on every launch that isn't the first one. 
          Log.w("SpotTheStation", "Error while creating db: " + e.toString());
      }       
  }
  </code></pre>
  
  <hr>
  
  <p>I am using SQLite on my android application and seem to be running into a very strange issue. It seems like every single time I call <code>getWriteableDatabase()</code> the system attempts to recreate the whole database, i.e it calls <code>onCreate()</code>, and this is causing an SQLiteException. </p>
  
  <p>Here's the full error message: </p>
  
  <pre><code>03-21 14:36:44.082: V/SpotTheStation(20034): android.database.sqlite.SQLiteException:, 
  while compiling: create table countries(_id integer primary key autoincrement,
  country text);
  </code></pre>
  
  <p>This happens every time I try to access DBdata, I have a separate static class that handles DB access throughout the app lifetime, I pass the context to it the following way: </p>
  
  <pre><code>public static Cursor getCountries (final Context context) {
  
      if (mDatabase == null || !mDatabase.isOpen()) {
          Constants.logMessage("DB was null, opening");
          open(context);
      }
  
      return mDatabase.query(CountryDbHelper.TABLE_COUNTRIES, CountryDbHelper.PROJECTION_COUNTRIES, null, null, null, null, CountryDbHelper.DEFAULT_SORT_ORDER);          
  }
  </code></pre>
  
  <p>And everytime I call this method, the log states that is in fact opening the database, which is fine. Here's how I open the database: </p>
  
  <pre><code>private static void open (Context context) {
      if (mDatabase != null &amp;&amp; mDatabase.isOpen()) {
          return;
      }
      try {
          mHelper = new CountryDbHelper(context);
          mDatabase = mHelper.getWritableDatabase();
      } catch (Exception e) {
          Constants.logMessage(e.toString());
      }
  }
  </code></pre>
  
  <p>And that is the statement that is causing the exception, as getWriteableDatabase should have a cached Database, or should call <code>onOpen()</code> rather than <code>onCreate()</code>. </p>
  
  <p>Here's the create statement that seems to be executed on every db access call: </p>
  
  <pre><code>private static final String DATABASE_CREATE = "create table " + TABLE_COUNTRIES 
          + "(_id integer primary key autoincrement, " 
          + "country text);"
          ;
  </code></pre>
  
  <p>And here's the <code>onCreate()</code> which is rather simple: </p>
  
  <pre><code>@Override
  public void onCreate(SQLiteDatabase db) {
      Constants.logMessage("Creating database from scratch?");
      db.execSQL(DATABASE_CREATE);
  }
  </code></pre>
  
  <p>I added the log messages to trace the problem, here's the progression: </p>
  
  <pre><code>03-21 14:36:44.004: V/SpotTheStation(20034): DB was null, opening
  03-21 14:36:44.035: V/SpotTheStation(20034): Creating database from scratch?
  03-21 14:36:44.082: V/SpotTheStation(20034): android.database.sqlite.SQLiteException: table countries already exists (code 1): , while compiling: create table countries(_id integer primary key autoincrement, country text);
  </code></pre>
  
  <p>Now, Here's what bothers me, i have the same DB Structure on a few other applications (same logic, a non-static OpenHelper, and a static Manager to access the dB contents), and everything works fine, not sure what is causing the problem here. </p>
  
  <p>I'd appreciate some help. </p>
  
  <p><strong>QUICK EDIT</strong></p>
  
  <p>To clear things up a bit more, the <code>getCountries</code> <strong>does</strong> work at first boot (the first time the app is run), subsequent runs will result in the exception. A uninstall reinstall process makes it work once again, and then at second run it dies. </p>
  
  <p><strong>EDIT NUMBER TWO</strong> </p>
  
  <p>As per request, here's the full code of the OpenHelper: </p>
  
  <pre><code>public class CountryDbHelper extends SQLiteOpenHelper {
  
  /**
   * This path is hardcoded, during the first time application opens
   * we copy a database file from the assets folder into this folder. 
   */
  public final static String DB_PATH = "/data/data/com.nasa.apps.spotthestation/databases/";
  private static final int DB_VERSION = 2;
  public static String DB_NAME = "countries";
  public static final String TABLE_COUNTRIES = "countries";
  public static final String TABLE_REGIONS = "regions";
  public static final String DEFAULT_SORT_ORDER = "_id";
  public static final String [] PROJECTION_COUNTRIES = {
      "_id",
      "country"
  };
  
  public static final String [] PROJECTION_REGIONS = {
      "_id",
      "country",
      "region"
  };
  
  /**
   * Table with only countries
   */
  private static final String CREATE_TABLE_COUNTRIES = "create table " + TABLE_COUNTRIES 
          + "(_id integer primary key autoincrement, " 
          + "country text);"
          ;
  
  /**
   * Table that contains regions per country
   */
  private static final String CREATE_TABLE_REGIONS = "create table " + TABLE_REGIONS 
          + "(_id integer primary key autoincrement, " 
          + "country text, "
          + "region text);"
          ;
  
  /**
   * Not used for now
   */
  private static final String CREATE_TABLE_CITIES = "create table " + TABLE_COUNTRIES 
          + "(_id integer primary key autoincrement, " 
          + "country text, "
          + "region text, "
          + "city text);"
          ;
  
  public CountryDbHelper(Context context, CursorFactory factory,
          int version) {
      super(context, DB_NAME, factory, version);
  }
  
  public CountryDbHelper (Context context) {
      super(context, DB_NAME, null, DB_VERSION );
  }
  
  @Override
  public void onCreate(SQLiteDatabase db) {
      Constants.logMessage("Creating database from scratch?");
      //Create both of the tables as part of the databases
      db.execSQL(CREATE_TABLE_COUNTRIES);
      db.execSQL(CREATE_TABLE_REGIONS);
  }
  
  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
      Constants.logMessage("Upgrading db from " + oldVersion + " to new version: " + newVersion);
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_COUNTRIES);
      db.execSQL("DROP TABLE IF EXISTS " + TABLE_REGIONS);
      onCreate(db);
  }
  }
  </code></pre>
  
  <p>Also, one thing I noticed is that the database file on /data/data/packageName/databases/ doesn't have a "sqlite_sequence" file, which usually stores the count of rows per each table on the database. I think the problem may be that file missing, since the two tables use "PRIMARY KEY autoincrement", and I believe it relies on that to know what is the next _id to use when inserting. Any one can confirm this? </p>
  
  <p><strong>EDIT AGAIN</strong> Just in case, I went on changed the data type from <code>autoincrement</code> to simply <code>numeric</code> on the "_id" field, since I do not plan to add more data to the database, it is a read-only intent, but it still force closes on the second launch. </p>
  
  <p>Thanks!</p>