title: SQLite database is read only
body: <p>I have some code that copies a database from <code>asset</code> to <code>/data/data</code>.</p>
  
  <p>When I use <code>SELECT</code> query, it works; but when I use <code>INSERT</code> query, it returns an error saying that my database is read only.</p>
  
  <p>My code:</p>
  
  <pre><code>public class AssetDatabaseOpenHelper {
  
      private static final String DB_NAME = "database";
  
      private Context context;
  
      public AssetDatabaseOpenHelper(Context context) {
          this.context = context;
      }
  
      public SQLiteDatabase openDatabase() {
          File dbFile = context.getDatabasePath(DB_NAME);
  
          if (!dbFile.exists()) {
              try {
                  copyDatabase(dbFile);
              } catch (IOException e) {
                  throw new RuntimeException("Error creating source database", e);
              }
          }
  
          return SQLiteDatabase.openDatabase(dbFile.getPath(), null, SQLiteDatabase.OPEN_READONLY);
      }
  
      private void copyDatabase(File dbFile) throws IOException {
          InputStream is = context.getAssets().open(DB_NAME);
          OutputStream os = new FileOutputStream(dbFile);
  
          byte[] buffer = new byte[1024];
          while (is.read(buffer) &gt; 0) {
              os.write(buffer);
          }
  
          os.flush();
          os.close();
          is.close();
      }
  }
  </code></pre>
  
  <p>Code using my class in my activity:</p>
  
  <pre><code>AssetDatabaseOpenHelper adb = new AssetDatabaseOpenHelper(context);
  SQLiteDatabase db = adb.openDatabase();
  </code></pre>