Note that there are some explanatory texts on larger screens.

plurals
  1. POUnable to copy SQLite database from assests folder to device memory
    primarykey
    data
    text
    <p>I'm Unable to copy SQLite database from assests folder to device memory(trying on emulator).</p> <p>I have a database in my project's assests folder which has a table containing 1000s of <strong>pre-existing rows</strong>.</p> <p>I intend to copy the existing file from the assets folder into the database folder of emulator.</p> <p><strong>Piece of ACTIVITY</strong></p> <pre><code> @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //All views(list,textboxes) are declared. try { dbM = new DbManager(this); dbM.checkDataBase(); try { dbM.createDataBase(); } catch (Exception e) { throw new Error(" *** ERROR in DB Access *** " + e.getMessage()); } dbM.openDB(); symbolarr = dbM.getSymbol(); lv.setAdapter(new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, symbolarr)); } catch (Exception e) { throw new Error(" *** ERROR in onCreate *** " + e.getMessage()); } finally { dbM.close(); } } </code></pre> <p><strong>Piece of code from my DbManager class:</strong></p> <pre><code> public boolean checkDataBase() { String myPath = DATABASE_PATH + DATABASE_NAME; File f = new File(myPath); return f.exists(); } public void createDataBase() { try { InputStream myInput = ctx.getAssets().open(DATABASE_NAME); OutputStream myOutput = new FileOutputStream(DATABASE_PATH + DATABASE_NAME); byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer)) &gt; 0) { myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); } catch (FileNotFoundException e) { throw new Error("file not found -- " + e.getMessage()); } catch (IOException e) { throw new Error("io exception " + e.getMessage()); } catch (Exception e) { throw new Error(" exception " + e.getMessage()); } } public DbManager openDB() throws SQLException { dbmgr = new DbManager(ctx); mDb = dbmgr.getWritableDatabase(); return this; } public String[] getSymbol() { Cursor cur; try { cur = mDb.rawQuery("select symbol,company_name from Scrip", null); } catch (SQLiteException e) { throw new Error(" *** ERROR in cursor *** " + e.getMessage()); } String[] b1 = new String[1326]; int x = 0; if (cur.moveToFirst()) { do { b1[x] = cur.getString(cur.getColumnIndex("symbol")); x++; } while (cur.moveToNext()); } cur.close(); return b1; } </code></pre> <p><strong>LOGCAT</strong></p> <pre><code> FATAL EXCEPTION: main java.lang.Error: file not found -- AndroidDB.db at com.dbexample.DbManager.createDataBase(DbManager.java:113) at com.dbexample.DataAttach.onCreate(DataAttach.java:83) at android.app.Activity.performCreate(Activity.java:4465) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980) at android.app.ActivityThread.access$600(ActivityThread.java:122) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:4340) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) at dalvik.system.NativeStart.main(Native Method) </code></pre> <h2>NOTE</h2> <p>1: Changing buffer size doesn't help..</p> <p>2: There is a file named AndroidDB.db with size 62kb in my assets folder..</p> <p>3: both the database names(in my assets folder and in my code) are same and I have android_metadata table in my DB which is in assets folder..</p> <p><strong>4: When I was not using <em></strong><code>createDataBase()</code><strong></em>, Database was getting created but my desired table i.e.Scrip was not getting copied. So when I tried to fetch the data from table, I was getting an exception that <code>no such table Scrip</code>.... That means I need to copy <code>Scrip</code> table from assets folder to database present on emulator memory. When I try to do the same using <code>createDataBase()</code>,I'm getting nullPointerException</strong></p> <p>5.: When I tried following code in createDatabase()</p> <pre><code>AssetManager assetManager = ctx.getAssets(); String[] files = assetManager.list("Files"); for (int i = 0; i &lt; files.length; i++) { str[i] = "\n=" + " file=" + " :" + i + "=" + " name=" + "&gt; " + files[i]; } Log.v("len=", "" + files.length); </code></pre> <p>Then files.length is equal to <code>0</code>. That simply means <strong>It is unable to detect any files inside the assets folder.</strong></p> <p><em><strong>ANY HELP WILL BE LIFE-SAVER !!!</em></strong></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.
 

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