Dear gurus, help determine the choice.

The application base should contain about 500 entries. Which method of filling is better to choose?

1) save the database structure and its data as SQL scripts, include these scripts in the application

2) transfer the database as part of the application as a SQLite database file.

  • Yes, as Hosh and do. too broad question - arg
  • 7
    I do not understand why minus a newbie that it was hard to just ask what the boy needs? - Barmaley

2 answers 2

It is best to prepare the database in advance in a third-party desktop application (for example, SLQite Browser ) and throw it into assets and from there, when you first start the application, copy via:

 context.getAssets().open(DATABASE_NAME); 

Otherwise, creating a database from scripts and inserting 500 records when you first start the application is not the most fun.

  • Cool, did not have time to write an answer)))) - Saidolim

I decided so

  1. Created a database locally on a PC using SQLiteBrowser

  2. Saved base file (binary file) in assets

  3. Then at the first start I copied the base to the phone.

     public void initialise() { if (mInstance == null) { if (!checkDatabase()) { copyDataBase(); } mInstance = new AssetDatabaseOpenHelper(mContext, null, DATABASE_VERSION); mSqliteDb = mInstance.getWritableDatabase(); } } 

and how copied

 private static void copyDataBase() { try { // Open your local db as the input stream InputStream myInput = mContext.getAssets().open(DATABASE_NAME); // Path to the just created empty db String outFileName = getDatabasePath(); // if the path doesn't exist first, create it File f = new File(mContext.getApplicationInfo().dataDir + DB_PATH_SUFFIX); if (!f.exists()) f.mkdir(); // Open the empty db as the output stream OutputStream myOutput = new FileOutputStream(outFileName); // transfer bytes from the inputfile to the outputfile byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer)) > 0) { myOutput.write(buffer, 0, length); } // Close the streams myOutput.flush(); myOutput.close(); myInput.close(); } catch (Exception e) { e.printStackTrace(); } }