I’ve followed the instructions given here for introducing an existing SQLite database to your Android app.
When I query the table “android_metadata” this is fine. But when I run a similar query on my own table “words” (which has _id for primary integer key) I get a table does not exist exception and the app crashes.
Why is that?
Code:
Cursor c = myDatabase.query("android_metadata", null, null, null, null, null, null, null);
works but
Cursor c = myDatabase.query("words", null, null, null, null, null, null, null);
returns a table does not exist exception.
This is how I’m creating the database (the references to paths and filenames are correct):
private void copyDatabase() throws IOException
{
//Open local db as the input stream
InputStream myInput = mContext.getAssets().open(DB_NAME);
//Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//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 = 0;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
(Note: To my eyes, the table is there. I’m looking right at it in my SQLite browser.)
You’re following a red herring. The android_metadata table is created in every android database no matter what.
The real mechanism for determining your issue is to simply run your app in the emulator and check out the database. If your emulator is running and the application has setup the database, run:
if
lsshows the database name you expect, run:at the sqlite3 prompt run :
.schemaThis will print out the tables of the databases. My guess is that you’ll find just the meta data table because android is not actually reading your database from your external location. If thats the case, comment back and I can try to help you through that process.