Note that there are some explanatory texts on larger screens.

plurals
  1. POandroid Could not open database - several different errors
    primarykey
    data
    text
    <p>I am writing this cause I had enough....</p> <p>I could not take it anymore...</p> <p>I read &amp; read &amp; read...&amp; then, read a little more...just like before I wrote this question here!</p> <p>I am trying to copy a ready made sqlite DB from assets to app DB folder.</p> <p>I have tried all combinations of copying code, path code &amp; more.</p> <p>This is from SO:</p> <pre><code> /** * http://stackoverflow.com/questions/10738623/copy-database-from-assets-folder-in-unrooted-device * Copies your database from your local assets-folder to the just created * empty database in the system folder, from where it can be accessed and * handled. This is done by transfering bytestream. * */ public static void copyDataBase(String dbname, Context mContxt) throws IOException { myContext = mContxt; // Open your local db as the input stream InputStream myInput = myContext.getAssets().open(dbname); // Path to the just created empty db // String outFileName = "/data/data/" + MainActivity.PACKAGE_NAME + "/databases/" + dbname; String outFileName1 = "/data/data/" + MainActivity.PACKAGE_NAME; // + "/databases/" + dbname; String outFileName = myContext.getFilesDir().getAbsolutePath().replace("files", "databases/"); // + "/Database/"; File newDir = null; File dir = new File(myContext.getApplicationInfo().dataDir+"/databases"); dir.mkdirs(); if (!dir.isDirectory()) { Log.v(TAG, "NOTaDIrectory"); newDir = new File(outFileName); newDir.mkdir(); if(newDir.exists()) { if (newDir.isDirectory()) { Log.v("isDIrectory", newDir.toString()); } } } else { Log.v(TAG, "NOTaDIrectory"); } File outFileName1Dir = new File(outFileName1); if (!outFileName1Dir.isDirectory()) { newDir = new File(outFileName); newDir.mkdir(); if(newDir.exists()) { if (newDir.isDirectory()) { Log.v("isDIrectory", newDir.toString()); } } } else { Log.v(TAG, "ISaDIrectory"); } Log.v(TAG, outFileName + dbname); File file = new File(outFileName + dbname); // File file = myContext.getFileStreamPath(dbname); Log.v(TAG, "" + file.getPath() + " - " + file.getTotalSpace()); Log.v(TAG, outFileName + dbname); File ff = new File(outFileName , dbname); if (ff.exists()) { Log.v(TAG, "FF -----------EXISTS"); } String[] files = myContext.fileList(); for (String file1 : files) { Log.v(TAG, file1.toString()); if (file1.equals(dbname)) { Log.v(TAG, "FILE EXISTS"); } } if (file.exists()) { Log.v(TAG, "FILE EXISTS"); if(file.isFile()) { Log.v(TAG, "isFILE"); // return; } } else { Log.v(TAG, "NO FILE"); // 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)) &gt; 0) { myOutput.write(buffer, 0, length); Log.v(TAG, "Copying DB"); } Log.v("FileExists", "" + file.createNewFile()); Log.v("FileWriteAble", "" + file.canWrite()); // Close the streams myOutput.flush(); myOutput.close(); myInput.close(); if (!file.canWrite()) { Log.v("FileExists", "" + file.setWritable(true)); } Log.v(TAG, "" + file.getPath() + " - " + file.getTotalSpace()); } } </code></pre> <p>I know...its a bunch of code but some people would ask to see it....</p> <p>JUST CANT SEEM TO WRAP MY HEAD AROUND THE PROBLEM.</p> <p>Some basic code from MainActivity that call this function:</p> <pre><code>public class MainActivity extends FragmentActivity { Context myContext; String DB_PATH = null; String DB_NAME = null; public static ContextWrapper cw = null; public static String PACKAGE_NAME; </code></pre> <p>After that, the call and some other defs: **Most of the commented lines were used in different attempts to trying and accomplish this task...</p> <pre><code> @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); myContext = getApplicationContext(); // ContextWrapper cw = new ContextWrapper(getApplicationContext()); // DB_PATH =cw.getFilesDir().getAbsolutePath()+ "/Database/"; DB_NAME = "dates1.sqlite"; // DB_PATH = cw.getFilesDir().getAbsolutePath().replace("files", "Databases/"); // + "/Database/"; /* DB_PATH = destPath.substring(0, destPath.lastIndexOf("/")) + "/databases"; Log.v(TAG, DB_PATH); */ // get package name from anywhere PACKAGE_NAME = getApplicationContext().getPackageName(); /* DbHelperNew dbNew = new DbHelperNew(cw, DB_NAME, null, 1); Boolean T = dbNew.checkDataBase(); Log.v(TAG, T.toString()); */ String[] files = this.fileList(); for (String file : files) { Log.v(TAG, file.toString()); if (file.equals(DB_NAME)) { Log.v(TAG, "FILE EXISTS"); } } try { DbUtils.copyDataBase(DB_NAME, myContext); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } // DbUtils.copyDataBase(DB_NAME, DB_PATH, myContext); /* File src = new File("file:///android_asset/" + DB_NAME); File dest = new File(DB_PATH + DB_NAME); DbUtils.movedb(src, dest); */ for (String file : files) { Log.v(TAG + "SECOND", file.toString()); if (file.equals(DB_NAME)) { Log.v(TAG, "FILE EXISTS"); } } /* END */ _dbHelper = new DbHelper(getApplicationContext()); db = _dbHelper.getReadableDatabase(); </code></pre> <p>I do apologise for the messy code...just to show the different attempts...</p> <p>ERROR msgs:</p> <pre><code>09-23 09:21:45.609: V/DbUtils(12851): Copying DB 09-23 09:21:45.621: W/System.err(12851): java.io.IOException: open failed: ENOTDIR (Not a directory) 09-23 09:21:45.628: W/System.err(12851): at java.io.File.createNewFile(File.java:940) 09-23 09:21:45.640: W/System.err(12851): Caused by: libcore.io.ErrnoException: open failed: ENOTDIR (Not a directory) 09-23 09:21:45.699: E/SQLiteOpenHelper(12851): Couldn't open dates1.sqlite for writing (will try read-only): 09-23 09:21:45.699: E/SQLiteOpenHelper(12851): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database 09-23 09:21:45.707: E/SQLiteLog(12851): (14) cannot open file at line 30174 of [00bb9c9ce4] 09-23 09:21:45.707: E/SQLiteLog(12851): (14) os_unix.c:30174: (20) open(/data/data/com.ndroidians.listfrag/databases/dates1.sqlite) - 09-23 09:21:45.710: E/SQLiteDatabase(12851): Failed to open database '/data/data/com.ndroidians.listfrag/databases/dates1.sqlite'. 09-23 09:21:45.710: E/SQLiteDatabase(12851): android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database </code></pre> <p>In order to solve the different error issues, I did:</p> <ol> <li>Try different directories, inc.: code for different OS vers, different FOLDER/ PACKAGE accessing code, creating the Dir. </li> <li>Changed file extension from */db to *.sqlite</li> <li>Changed file permissions to 777 in assets folder </li> <li>Tried different copy functions</li> <li>Tried different Phones</li> </ol> <p>I might have forgotten something along the way but it has been 2 long days that I am cracking my head over this simple (seemingly) task...</p> <p>HELP....</p> <p><strong><em>EDIT</em></strong></p> <p>Some more info:</p> <p>I have trid to chown as suggested...still got the same error:</p> <p>Emulator (this time)</p> <pre><code>&gt;= 4.2 API17 New database is being copied to device! dates1.sqlite New database has been copied to device! chown 10054:10054 /data/data/com.ndroidians.listfrag/databases/dates1.sqlite AFTER PROCESS (14) cannot open file at line 30176 of [00bb9c9ce4] (14) os_unix.c:30176: (20) open(/data/data/com.ndroidians.listfrag/databases/dates1.sqlite) - Failed to open database '/data/data/com.ndroidians.listfrag/databases/dates1.sqlite'. android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database </code></pre> <p>The funny thing about it is that on <strong>Emulator</strong> it shows that the databases folder is not a folder but kind of a file... I attach a picture....<img src="https://i.stack.imgur.com/in5YY.png" alt="Emulator_File_Explorer"></p> <p>On the other hand on a <strong>device</strong> its show Data as correct size in App Info. I attach a screen capture.... <img src="https://i.stack.imgur.com/uHy1C.png" alt="Nexus S OS-4.1.2_ScreenCaptuer_App_Info"></p> <p>This is a riddle....anyone???</p>
    singulars
    1. This table or related slice is empty.
    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. 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