Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From my development experience , I always prefer to add a prepared sqlite database file in the /res/raw folder.You create/manage sqlite database using Sqlite Manager addon of Firefox , it's a great tool. This method is really great because </p> <ul> <li>firstly I don't need to write a bunch of codes for creating/managing database.</li> <li>Most importantly , some applications needs to read from a pre-populated database. I don't need to care about what the app requires and whether database is empty or filled already. It serves all purpose. I just need to write some methods that runs the required simple sqls.</li> </ul> <p>Here is my own customised DatabaseHelper class. To use this class you'll need to follow some instructions.</p> <ol> <li>If sqlite database size is more than 1MB then split the file into chunks , I prefer 512KB chunks and place them into /res/raw directory.</li> <li><p>Edit the package name and your db file names in the following class.</p> <pre><code>package your.packagee.name; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import android.content.Context; import android.content.res.Resources; import android.database.Cursor; import android.database.SQLException; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteException; import android.database.sqlite.SQLiteOpenHelper; import android.util.Log; import android.widget.Toast; public class DataBaseHelper extends SQLiteOpenHelper { private static final String pkg = "your package name"; private static String DB_PATH = "/data/data/" + pkg + "/databases/"; private static String DB_NAME = "yourDBFile.sqlite"; int[] dbfiles = { R.raw.chunk1 , R.raw.chunk2 ..... }; private SQLiteDatabase myDataBase; private final Context myContext; public DataBaseHelper(Context context) { super(context, DB_NAME, null, 1); this.myContext = context; } public void createDataBase() { boolean dbExist = checkDataBase(); if (dbExist) { // do nothing - database already exist } else { this.getReadableDatabase(); try { CopyDataBase(); } catch (IOException e) { Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT) .show(); Log.d("Create DB", e.getMessage()); } } } private boolean checkDataBase() { SQLiteDatabase checkDB = null; try { String myPath = DB_PATH + DB_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); } catch (SQLiteException e) { Toast.makeText(myContext, e.getMessage(), Toast.LENGTH_SHORT) .show(); Log.d("Check DB", e.getMessage()); } if (checkDB != null) { checkDB.close(); } return checkDB != null ? true : false; } private void CopyDataBase() throws IOException { InputStream databaseInput = null; Resources resources = myContext.getResources(); String outFileName = DB_PATH + DB_NAME; OutputStream databaseOutput = new FileOutputStream(outFileName); byte[] buffer = new byte[512]; int length; for (int i = 0; i &lt; dbfiles.length; i++) { databaseInput = resources.openRawResource(dbfiles[i]); while ((length = databaseInput.read(buffer)) &gt; 0) { databaseOutput.write(buffer, 0, length); databaseOutput.flush(); } databaseInput.close(); } databaseOutput.flush(); databaseOutput.close(); } public void openDataBase() throws SQLException { String myPath = DB_PATH + DB_NAME; myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS); } @Override public synchronized void close() { if (myDataBase != null) myDataBase.close(); super.close(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public boolean deleteItem (String ID){ String query = "delete from item where id='" + ID + "'" ; Log.d("Query : ", query); try{ myDataBase.execSQL(query); return true ; } catch (Exception e){ Log.d("Exception", e.toString()); return false ; } } public Cursor getSearchFromID(String id) { return myDataBase.rawQuery("select * from item where id = \"" + id + "\"", null); } public boolean addSave(String type, String data , String time) { String query = "insert into item (type, data , timestamp) values ('" + type + "', '" + data + "', '" + time + "')"; try { myDataBase.execSQL(query); return true ; } catch (Exception e) { return false ; } } } </code></pre></li> </ol> <p>Here's some methods written as a sample , how to use it.</p> <p>Usage is simple. When your application starts , that means in your Launcher activity use this code to initialize your database</p> <pre><code>DataBaseHelper helper = new DataBaseHelper(this); helper.createDataBase(); helper.openDataBase(); helper.close(); </code></pre> <p>Then just use the methods written in DatabaseHelper class. A sample will be like this</p> <pre><code> String id = "1"; DataBaseHelper helper = new DataBaseHelper(this); helper.openDataBase(); Cursor c = helper.getSearchFromID(id); if(c.getCount() &gt; 0){ c.moveToFirst(); while(!c.isAfterLast()){ // extract your data from cursor c.MoveToNext(); } } </code></pre> <p>Hope it will solve your all problems about sqlite database in Android. At least it solved for me. Thank you.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload