I'm new to Android development. I found a lot of information on the Internet about how to create databases, manage, make any changes, but I need to create a database separately, and deliver it already created with my application, that is, it must somehow be packaged in apk. In this case, everything should happen without connecting to the Internet.

Is it even possible to do this? If so, how is this better implemented?

  • one
    The task is trivial. Here everything is described in great detail here and here - Roman Novoselov
  • I advise you to drill the Internet further, otherwise you quickly surrendered - iFr0z
  • @ iFr0z ++ actually was info when it was late - Amir Shabanov
  • Here's what he found himself - Amir Shabanov

1 answer 1

Create a database, put it in the assets , at startup check and copy the database if it is not found. SQLiteOpenHelper :

  private static class DatabaseHelper extends SQLiteOpenHelper { public DatabaseHelper(Context context, String name, int version) throws IOException { super(context, name, null, version); File dbFile = context.getDatabasePath(name); if (!dbFile.exists()) { dbFile.getParentFile().mkdirs(); InputStream is = context.getAssets().open(name); OutputStream os = new FileOutputStream(dbFile); byte[] buffer = new byte[1024]; while (is.read(buffer) > 0) { os.write(buffer); } os.flush(); os.close(); is.close(); } } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }