title: Why has my Android SQLite DB gone corrupt all of a sudden?
body: <p>All of a sudden, it seems the database that my application is using has gone corrupt.
  I didn't change the structure of the database, but I did re-deploy the app several times on my device today.</p>
  
  <p>It is throwing the following exception.</p>
  
  <pre><code>E/Database(14281): CREATE TABLE android_metadata failed err=26 ..
  E/Database(14281): Failed to setLocale() when constructing, closing the database
  E/Database(14281): android.database.sqlite.SQLiteException: file is encrypted or is not  a database
  E/Database(14281):  at android.database.sqlite.SQLiteDatabase.native_setLocale(Native  Method)
  E/Database(14281):  at android.database.sqlite.SQLiteDatabase.setLocale(SQLiteDatabase.java:1848)
  E/Database(14281):  at android.database.sqlite.SQLiteDatabase.&lt;init&gt;(SQLiteDatabase.java:1798)
  E/Database(14281):  at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:798)
  E/Database(14281):  at   android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:857)
  E/Database(14281):  at android.database.sqlite.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:850)
  E/Database(14281):  at android.app.ApplicationContext.openOrCreateDatabase(ApplicationContext.java:539)
  E/Database(14281):  at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
  E/Database(14281):  at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:98)
  E/Database(14281):  at com.ecs.android.gps.storage.DBAdapter.open(DBAdapter.java:75)
  </code></pre>
  
  <p>The database is initialized using the SQLLiteOpenHelper class.</p>
  
  <pre><code>public DBAdapter(Context ctx) 
  {
      this.context = ctx;
      DBHelper = new DatabaseHelper(context);
  }
  
  private static class DatabaseHelper extends SQLiteOpenHelper 
  {
      DatabaseHelper(Context context) 
      {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
      }
  
      @Override
      public void onCreate(SQLiteDatabase db) 
      {
          db.execSQL(DATABASE_CREATE);
      }
  
      @Override
      public void onUpgrade(SQLiteDatabase db, int oldVersion, 
      int newVersion) 
      {
          Log.w(TAG, "Upgrading database from version " + oldVersion 
                  + " to "
                  + newVersion + ", which will destroy all old data");
          db.execSQL("DROP TABLE IF EXISTS location_history");
          onCreate(db);
      }
  }  
  </code></pre>
  
  <ul>
  <li>I'm basically opening/closing both
  the database and cursors on each
  operation. </li>
  <li>Some articles indicate
  this being the way to go, but for an
  android app, wouldn't make more sense
  to keep the database connection open
  all the time ? </li>
  <li>I can imagine it
  taking a lot more resources to
  open/close it each time. </li>
  <li>There are multiple Activities and Services that
  access the database.  (potentially at the same time)</li>
  <li>I never saw an exception in the logs</li>
  </ul>
  
  <p>Any idea what could have caused this, how to restore from it, and how to prevent this in the future ?
  I have the impression the exception is coming due to some concurrency issues (multiple threads trying to open/close or read/write to the database). I don't know to what degree an SQLite database is as robust as a normal database (mysql - oracle) when it comes to multi-threaded access, locking, .....</p>