Note that there are some explanatory texts on larger screens.

plurals
  1. POContinued issue with opening a pre-existing sqlite database in Android
    text
    copied!<p>Newbie here. I received some good info in a previous post I made, and I'm hoping I can get a bit more help :) Thanks for looking.</p> <p>I am shipping a prepopulated sqlite database with an application. I've gone through the process of copying the database from my assets folder within the source code to the /database/ folder through a routine I found online, that I'm sure you all are familiar with (http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/). This appears to be working correctly. Using DDMS I am able to see the database being copied over and showing up in the proper directory. </p> <p>Now however, I am getting no results. When I examine logcat, I see the following errors when I attempt to open the DB:</p> <pre><code>02-19 14:59:40.490: I/Database(4071): sqlite returned: error code = 14, msg = cannot open file at source line 25467 02-19 14:59:40.490: E/Database(4071): sqlite3_open_v2("/data/data/com.testapp.test/databases/pbaMainDb.sqlite", &amp;handle, 2, NULL) failed </code></pre> <p>EDIT: Here is the ListScreen class I am using to open the database after installation and do a simple query from:</p> <pre><code>public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.lists); DbHelper db = new DbHelper(this); db = new DbHelper(this); try { db.createDatabase(); } catch (IOException ioe) { throw new Error ("Unable to create database"); } try { myDbHelper.close(); db.openDataBase(); } catch(SQLException sqle){ throw sqle; } } public Cursor fetchAllTables() { return myDbHelper.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", new String[]{}); } private void fillData() { //Cursor c = fetchAllTables(); //startManagingCursor(c); String[] from = new String[] {"name"}; int[] to = new int[] {R.id.listview}; SimpleCursorAdapter notes = new SimpleCursorAdapter (this, R.layout.list_view, cursor, from, to); setListAdapter(notes); } } </code></pre> <p>EDIT: Also, from my DbHelper class (where the code is to copy my existing Db), here is the code where I do that (I apologize, it's a bit of a dump):</p> <pre><code> public class DbHelper extends SQLiteOpenHelper { // the default android path of the database files it creates private static String DB_PATH = "/data/data/com.testdata.test/databases/"; private static String DB_NAME = "pbaMainDb.sqlite"; private SQLiteDatabase myDataBase; private final Context myContext; // constructor to keep a reference of the passed context in order to access the public DbHelper (Context context) { super(context, DB_NAME, null, 1); this.myContext = context; } // Create an empty database and then rewrite with original public void createDatabase() throws IOException { boolean dbExist = CheckDataBase(); if (dbExist) { // if exists, do nothing }else { //when we call this method the empty database will be created into this.getReadableDatabase(); try { copyDataBase(); } catch (IOException e) { throw new Error("Error copying database"); } } this.close(); } // Check if the database already exists to avoid re-copy private boolean CheckDataBase() { SQLiteDatabase checkDB = null; try { String myPath = DB_PATH + DB_NAME; checkDB = SQLiteDatabase.openDatabase(myPath, null SQLiteDatabase.OPEN_READWRITE); //File dbfile = new File(myPath); //return dbfile.exists(); } catch (SQLiteException e) { //database doesn't exist yet } if (checkDB != null) { checkDB.close(); } return checkDB != null ? true : false; } // Copies database from assets folder to new empty folder private void copyDataBase() throws IOException { InputStream myInput = myContext.getAssets().open(DB_NAME); //path to just created DB String outFileName = DB_PATH + DB_NAME; OutputStream myOutput = new FileOutputStream (outFileName); //transfer bytes from input file to output file 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(); } public void openDataBase() throws SQLException { // open the database String myPath = DB_PATH + DB_NAME; myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); } @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) { } } </code></pre>
 

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